mrz-checkdigit
Validate the check digits in a passport or ID card machine-readable zone (MRZ), as defined by ICAO Document 9303. Supports all three layouts:
| Layout | Used on | Shape |
|---|---|---|
| TD1 | ID cards | 3 lines × 30 chars |
| TD2 | Older ID cards, visas | 2 lines × 36 chars |
| TD3 | Passports | 2 lines × 44 chars |
It checks the document number, date of birth and expiry date digits, the TD3 optional-data digit, and the composite digit. Pure Python, no dependencies, typed.
Use it after OCR to reject or retry a misread MRZ (0/O, 8/B, 1/I and similar) before it reaches your database.
Install
pip install mrz-checkdigit
Usage
from mrz_checkdigit import check_digit, is_valid, validate
mrz = [
"P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<",
"L898902C36UTO7408122F1204159ZE184226B<<<<<10",
]
is_valid(mrz) # True
check_digit("L898902C3") # 6
for r in validate(mrz):
print(r.field, r.expected, r.computed, r.ok)
# document_number 6 6 True
# birth_date 2 2 True
# expiry_date 9 9 True
# optional_data 1 1 True
# composite 0 0 True
The layout is detected from the number and length of the lines. validate raises ValueError for any other shape.
Command line
mrz-checkdigit 'P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<' 'L898902C36UTO7408122F1204159ZE184226B<<<<<10'
Lines can also be piped on stdin. Exit code is 0 when every digit matches, 1 when one fails, 2 for an unrecognised layout.
Repairing OCR misreads
OCR confuses O/0, I/1, B/8, S/5 and Z/2. Fields that can only contain digits can be mapped back directly; alphanumeric fields such as the document number can be repaired by trying confusable swaps against the check digit:
from mrz_checkdigit import fix_numeric, repair
fix_numeric("74O8I2") # '740812'
repair("L8989O2C3", "6") # 'L898902C3': O read for 0, one repair fits
repair("L898902C3", "6") # 'L898902C3': already valid, left alone
repair("AB5O8I23S", "0") # None: two equally good repairs, so ask for a new photo
A single check digit lets about one wrong reading in ten pass, so repair only returns a result when exactly one candidate needs the fewest changes. Why that rule matters: Correcting MRZ OCR errors with check digits.
Dates
MRZ dates are YYMMDD, so the century has to be inferred:
from datetime import date
from mrz_checkdigit import birth_date, expiry_date
birth_date("740812", date(2026, 11, 18)) # 1974-08-12: a birth date can't be in the future
expiry_date("350101", date(2026, 11, 18)) # 2035-01-01: the century closest to today
People born more than 100 years ago are read as infants; compare with the four-digit year in the visual zone if that matters. Details: Parsing MRZ dates of birth and expiry correctly.
The algorithm
Each character becomes a number (digits keep their value, A–Z = 10–35, < = 0), is multiplied by the repeating weights 7, 3, 1, and the check digit is the sum modulo 10.
Background
The full walkthrough, with the field positions, the composite digit and why OCR pipelines depend on it: How passport MRZ check digits work, with code to validate them.
Check digits catch accidental errors. They are not a security feature: anyone can compute valid digits. Proving a document is genuine takes authenticity checks or reading the signed NFC chip.
Limitations
- TD1 document numbers longer than 9 characters (the overflow into the optional field) are not handled yet.
- Only check digits are validated; field formats (dates, country codes) are not.
License
MIT
Metadata
Release files for mrz-checkdigit 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mrz_checkdigit-0.2.0.tar.gz | 7.4 kB | With provenance | Details for mrz_checkdigit-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mrz_checkdigit-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for mrz_checkdigit-0.2.0-py3-none-any.whl |
Total release size: 14.7 kB
Release files / mrz_checkdigit-0.2.0.tar.gz
| Download URL | mrz_checkdigit-0.2.0.tar.gz |
|---|---|
| Size | 7.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
09972f9ed044408461369fbd2afeac57955373e6821680686af65686eb5d6eb3
|
|
BLAKE2b-256 checksum How to use checksums |
d6e5ecb28a05e1cdfb7a67e52ad1dd67198b23d370f19bf9f16c5b21e0bc5800
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logRelease files / mrz_checkdigit-0.2.0-py3-none-any.whl
| Download URL | mrz_checkdigit-0.2.0-py3-none-any.whl |
|---|---|
| Size | 7.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
031908802fb5472838326e1560484faf82baad704924bb810542c7278d5875b3
|
|
BLAKE2b-256 checksum How to use checksums |
8bcc6c77ca631fa7c7a9700620f97610ec9b0a69d0683f04756009f7e4c768cc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logSecurity for mrz-checkdigit 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for mrz_checkdigit-0.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.