Skip to blog content

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.

Download .md

Computing a passport MRZ check digit, step by step (silent animation)
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”:

TD3 specimen (ICAO 9303)
P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<
L898902C36UTO7408122F1204159ZE184226B<<<<<10

The second line holds the data, in fixed positions:

PositionsFieldIn the specimen
1–9Document numberL898902C3
10Check digit for the number6
11–13NationalityUTO
14–19Date of birth (YYMMDD)740812
20Check digit for date of birth2
21SexF
22–27Expiry date (YYMMDD)120415
28Check digit for expiry9
29–42Optional dataZE184226B<<<<<
43Check digit for optional data1
44Composite check digit0

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?

  1. Convert each character to a number: digits keep their value, letters A–Z become 10–35, and the filler < is 0.
  2. Multiply each value by a repeating weight of 7, 3, 1, 7, 3, 1, …
  3. 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 O in a date is a 0. 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.

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.