Identifier detector
Portuguese NIF: format, validation and detection
Short answer
A Portuguese NIF has nine digits. The first digit is 1, 2, 3, 5, 6, 8 or 9, and the last is a modulus 11 check digit over the first eight digits weighted 9 to 2.
What it is
The Número de Identificação Fiscal is the Portuguese tax number. Maskflare's Portugal pack covers it as a tax ID, and the GDPR (EU/EEA) profile includes the pack.
In Maskflare this is the detector pt_nif, labelled "Portugal - Tax identification number (NIF)", category tax id, in the Portugal pack.
Format and layout
- Nine digits, with optional spaces: 123456789.
- The first digit must be one of 1, 2, 3, 5, 6, 8 or 9.
How the check works
Multiply the first eight digits by 9, 8, 7, 6, 5, 4, 3 and 2 and add them. Take the remainder modulo 11. The check digit is 0 when the remainder is 0 or 1, and 11 minus the remainder otherwise.
Worked example
- Digits 1 2 3 4 5 6 7 8 with weights 9 8 7 6 5 4 3 2 give 9, 16, 21, 24, 25, 24, 21, 16.
- The total is 156, and 156 divided by 11 leaves 2.
- 11 minus 2 is 9, the last digit of the example.
Valid and invalid examples
| Example | Result | Why |
|---|---|---|
123456789 | Valid | Matches the layout and passes the validator. |
123456780 | Invalid | The invalid example changes one character, the last character: 9 becomes 0. |
In running text the valid example reads, for instance, NIF 123456789. The invalid one is left alone: a value that fails the validator is treated as an ordinary string.
Regex and validation caveats
- Nine plain digits are common, so the detector requires one of five keywords before the number: NIF, contribuinte, número de identificação fiscal, tax id or VAT.
- The Portuguese social security number (NISS) is a different detector with eleven digits.
- A company NIF has the same layout, so it can be masked too.
Words that confirm it
The detector masks a match only when one of its keywords appears within 48 characters before the value. The catalogue lists 5: contribuinte, nif, número de identificação fiscal, tax id, vat.
How to detect it in LLM traffic
Portuguese NIF values reach an LLM inside ordinary work rather than as standalone data: a pasted block of HR or payroll rows, a customer-support thread, an onboarding or identity-check form, a spreadsheet someone wants summarised, or an email they want rewritten. The model needs none of the digits for any of those jobs, so the safest place to remove them is before the request leaves the company.
Maskflare reads the whole request body, including the system prompt, earlier messages and tool arguments, so a number buried in the fourth message of a long chat is found as readily as one in the latest message. Each match is replaced with a token such as mf_mask_d7e0 before the request goes out, and with response restoration turned on the real value is put back into the answer for the person who is allowed to see it.
If you build the detection yourself
- Strip spaces, dots and hyphens before running any arithmetic, but remember where they were so the original text can be replaced exactly.
- Match the shape with a pattern anchored on word boundaries, so that a long digit string is not read as several shorter Portuguese NIF candidates.
- Run the check from the section above. A failing check is almost always a typo or a made-up value; a passing one removes most look-alikes.
- Require one of the words listed above shortly before the value. Maskflare looks back 48 characters.
- Replace the value before the text is sent anywhere, and keep a mapping if the answer has to be restored afterwards.
How Maskflare handles it
- Protect now, Finance - Protect now, Healthcare and Developer: mask (a reversible token) when Portugal is a primary country.
- Monitor first: alert (counted, forwarded unchanged) when Portugal is a primary country.
The available actions are mask (reversible token), redact, partial, hash, format-preserving encryption, generalize, alert and block. Masked values are kept encrypted for 30 days so that answers can be restored, then they expire; prompts and responses are not logged. The full list of detectors is in the detector reference.
Compliance profiles that include it
One compliance profile includes the Portugal pack, and applying it turns this detector on: GDPR - EU/EEA. For the GDPR (EU/EEA) profile the Portugal pack is one of the country packs the administrator can choose from; the profile turns on the ones chosen. A profile is a floor: it never turns anything off or weakens a rule that is already stricter.
Frequently asked questions
Which digits can a NIF start with?
Maskflare accepts 1, 2, 3, 5, 6, 8 and 9. Numbers starting with 4 or 7 are rejected.
Do I need to write NIF before the number?
One of the keywords must appear shortly before the number. The detector looks back up to 48 characters.
Is the Portuguese NIF detector on by default?
It depends on the organisation's primary countries. It is part of the Portugal pack, which the default Protect now template turns on with the action mask when Portugal is a primary country.
Does a valid Portuguese NIF prove the person exists?
No. A check digit shows the value is well formed. Many valid-looking numbers have never been issued, and Maskflare masks them all the same.
Rules on this page describe what Maskflare's detectors and validators check. They are read from the detection catalogue and are not the specification of the body that issues the identifier. Examples are synthetic.