DEV Community

Ali Ahmed
Ali Ahmed

Posted on Originally published at aliahmed.si

How passport MRZ check digits work, with code to validate them

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":

```text title="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?

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**.



```python title="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
Enter fullscreen mode Exit fullscreen mode

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:

```python title="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](https://aliahmed.si/blog/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](https://aliahmed.si/blog/ocr-non-latin-scripts-arabic-urdu/)
* [Face match thresholds: false accepts vs false rejects](https://aliahmed.si/blog/face-match-threshold-far-frr/)
* [Face liveness detection explained](https://aliahmed.si/blog/face-liveness-detection-explained/)
* [A real-time KYC card verification pipeline over MQTT](https://aliahmed.si/blog/mqtt-kyc-verification-pipeline/)

I build computer vision and OCR systems for identity verification; more on the [home page](https://aliahmed.si/).

Check digits catch errors; the NFC chip proves authenticity: [how e-passport chip verification works](https://aliahmed.si/blog/nfc-epassport-chip-verification/).

---

Written by Ali Ahmed · [Original article](https://aliahmed.si/blog/passport-mrz-check-digits/)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)