From the notebook
How passport MRZ check digits work, with code to validate them
The two lines of chevrons on a passport are the machine-readable zone. How ICAO 9303 check digits work (weights 7, 3, 1), how to validate the document number, dates and composite digit in Python, and why OCR pipelines rely on them.
What the video shows
ICAO's specimen passport is scanned and the camera zooms into the second MRZ line, highlighting the document number L898902C3 and its printed check digit 6. The nine characters lift into columns and become values: digits keep their value, L becomes 21 and C becomes 12. The weights 7, 3, 1 repeat beneath them, and each pair is multiplied: 147, 24, 9, 56, 27, 0, 14, 36 and 3. A running sum adds them to 316, and 316 mod 10 is 6. The computed 6 lands on the printed 6 and matches. Misread one character, and the digit stops matching.
The machine-readable zone (MRZ) is the block of uppercase letters, digits and < chevrons at the bottom of a passport data page. It repeats the key fields in a fixed format defined by ICAO Document 9303, and it carries check digits: each important field is followed by one digit computed from it with weights 7, 3, 1. A KYC pipeline uses them to catch OCR mistakes and some tampering. If a single character in the document number is misread, its check digit almost always stops matching. Here’s how the algorithm works, with tested Python.
What does an MRZ look like?
A passport (format TD3) has two lines of 44 characters. This is ICAO’s own specimen, for the fictional state “Utopia”:
P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<< L898902C36UTO7408122F1204159ZE184226B<<<<<10
The second line holds the data, in fixed positions:
| Positions | Field | In the specimen |
|---|---|---|
| 1–9 | Document number | L898902C3 |
| 10 | Check digit for the number | 6 |
| 11–13 | Nationality | UTO |
| 14–19 | Date of birth (YYMMDD) | 740812 |
| 20 | Check digit for date of birth | 2 |
| 21 | Sex | F |
| 22–27 | Expiry date (YYMMDD) | 120415 |
| 28 | Check digit for expiry | 9 |
| 29–42 | Optional data | ZE184226B<<<<< |
| 43 | Check digit for optional data | 1 |
| 44 | Composite check digit | 0 |
ID cards use other layouts: TD1 has three lines of 30 characters and TD2 two lines of 36, but the check digit algorithm is the same.
How is a check digit calculated?
- Convert each character to a number: digits keep their value, letters
A–Zbecome 10–35, and the filler<is 0. - Multiply each value by a repeating weight of 7, 3, 1, 7, 3, 1, …
- Add the products and take the remainder after dividing by 10.
def check_digit(field: str) -> int: weights = (7, 3, 1) total = 0 for i, ch in enumerate(field): if ch.isdigit(): value = int(ch) elif ch.isalpha(): value = ord(ch.upper()) - ord("A") + 10 else: value = 0 total += value * weights[i % 3] return total % 10
For the specimen, check_digit("L898902C3") returns 6, check_digit("740812") returns 2, and check_digit("120415") returns 9, matching the printed digits.
What is the composite check digit?
The last digit of a TD3 line covers several fields at once: the document number with its check digit (positions 1–10), the date of birth with its digit (14–20), and the expiry date, optional data and their digits (22–43), joined together:
line2 = "L898902C36UTO7408122F1204159ZE184226B<<<<<10" composite = line2[0:10] + line2[13:20] + line2[21:43] assert check_digit(composite) == int(line2[43])
The composite catches errors that individual digits can miss, such as two misreads that happen to cancel out within one field.
Why does this matter for OCR?
MRZ text is printed in OCR-B, a font designed for machines, but real photos still produce confusions: 0 and O, 1 and I, 8 and B, 5 and S, and dropped or doubled chevrons. Check digits turn “the OCR might be wrong” into something you can test:
- Reject or retry a reading whose digits don’t validate, instead of storing a wrong passport number.
- Repair using position rules. Dates and check digits must be numeric, so an
Oin a date is a0. The document number can contain letters, so it needs the check digit to decide. - Cross-check against the visual zone. The MRZ name and dates should match what OCR reads from the printed fields above. A mismatch means either an OCR error or a tampered page, and both need review. This is one of the stages in how KYC identity verification works.
What check digits don’t prove
Check digits detect accidental errors. They are not security: anyone forging a document can compute valid digits with the same ten lines of code. Proving a passport is genuine takes authenticity checks or, better, reading the NFC chip in e-passports, whose data is cryptographically signed by the issuing country.
Related
- Why OCR struggles with Arabic, Urdu and other non-Latin scripts
- Face match thresholds: false accepts vs false rejects
- Face liveness detection explained
- A real-time KYC card verification pipeline over MQTT
I build computer vision and OCR systems for identity verification; more on the home page.
Check digits catch errors; the NFC chip proves authenticity: how e-passport chip verification works.