Identifier detector
Swedish personnummer: format, validation and detection
Short answer
A Swedish personnummer is a birth date plus a three-digit serial and a Luhn check digit. Maskflare accepts 10 digits or 12 digits with the century, and coordination numbers (samordningsnummer) where the day is raised by 60.
What it is
The personnummer identifies people in Sweden. Maskflare's Sweden pack covers it as a national ID, and the GDPR (EU/EEA) profile includes the pack.
In Maskflare this is the detector se_personnummer, labelled "Sweden - Personal identity number (personnummer)", category national id number, in the Sweden pack.
Format and layout
- YYMMDD-XXXX with ten digits, or YYYYMMDD-XXXX with twelve, for example 811218-9876.
- For a samordningsnummer the day has 60 added, so a day above 60 is reduced before the date check.
- The date must be valid in the 1900s or the 2000s.
How the check works
Drop the century if present, leaving ten digits. Apply the Luhn algorithm: double every second digit starting from the first, subtract 9 from results over 9, add everything, and require a multiple of 10.
Worked example
- Digits 8 1 1 2 1 8 9 8 7 6. Doubling the first, third, fifth, seventh and ninth digits and reducing results over 9 gives 7, 1, 2, 2, 2, 8, 9, 8, 5, 6.
- The total is 50, a multiple of 10, so the number passes.
Valid and invalid examples
| Example | Result | Why |
|---|---|---|
811218-9876 | Valid | Matches the layout and passes the validator. |
811218-9870 | Invalid | The invalid example changes one character, the last character: 6 becomes 0. |
In running text the valid example reads, for instance, personnummer 811218-9876. The invalid one is left alone: a value that fails the validator is treated as an ordinary string.
Regex and validation caveats
- The Luhn test is a weak filter on its own (one in ten random ten-digit numbers passes), so the date rule matters: it removes most random numbers before the check.
- A ten-digit run with no separator is masked unless a word like order or invoice sits closer to it than one of the detector's keywords (id no, id number, identity number).
Words that confirm it
The formatted shape is masked without a keyword. For a run with no separators, these words count as the detector's own keywords when a word like order, invoice or phone is also close: id no, id number, identity number, national id, personal id, personal identity number, personal number, personnr, personnummer, pnr. The nearer word wins.
How to detect it in LLM traffic
Swedish personnummer 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 Swedish personnummer 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.
- Treat a run with no separators more carefully than a formatted one: reject it when a word such as order, invoice, phone or reference is nearer to it than one of the identifier's own keywords.
- 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 Sweden is a primary country.
- Monitor first: alert (counted, forwarded unchanged) when Sweden 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 Sweden pack, and applying it turns this detector on: GDPR - EU/EEA. For the GDPR (EU/EEA) profile the Sweden 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
Does the detector accept a personnummer with a plus sign?
Yes. The pattern allows a hyphen or a plus sign (or neither) before the last four digits, and the validator works on the digits only.
What is a samordningsnummer?
A coordination number given to people who are not registered residents. It looks like a personnummer with 60 added to the day, and Maskflare's validator accepts it.
Is the Swedish personnummer detector on by default?
It depends on the organisation's primary countries. It is part of the Sweden pack, which the default Protect now template turns on with the action mask when Sweden is a primary country.
Does a valid Swedish personnummer 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.