Video: Computing a passport MRZ check digit, step by step
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":
TD3 specimen (ICAO 9303)
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.
mrz.py
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:
Validate the composite digit
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.
Check digits confirm the digits; they don't say which century a date is in. That's covered in parsing MRZ dates of birth and expiry.
Card numbers and IBANs use different check-digit schemes: see Luhn and IBAN check digits explained.
When the check digit fails because OCR misread a character, see correcting MRZ OCR errors with check digits.
Check digits catch misreads but can't prove a document is genuine; for what the chip adds, see OCR vs NFC chip reading.
To parse every other field of the MRZ around these digits, see parsing a passport MRZ in Python.
Written by Ali Ahmed (QuantumBreakz), AI engineer in Lahore · Original article
Top comments (3)
Clear step by step. The 7, 3, 1 weighting is a nice small example of how check digits catch the most common typos, swapped neighbours included. Good reminder that a valid check digit only means the line was typed right, not that the passport is real.
Appreciate it! That’s the key distinction I wanted to highlight: MRZ check digits are great at catching OCR and transcription mistakes, especially common single-character errors and many adjacent swaps, but they only validate the format and internal consistency of the data. Document authenticity still needs additional checks, such as visual/security-feature inspection and issuer or database verification.
Right, check digits catch typos, not forgeries. Good distinction to make explicit.