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.
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.1.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.1.0.tar.gz | 5.2 kB | With provenance | Details for mrz_checkdigit-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mrz_checkdigit-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for mrz_checkdigit-0.1.0-py3-none-any.whl |
Total release size: 10.5 kB
Release files / mrz_checkdigit-0.1.0.tar.gz
| Download URL | mrz_checkdigit-0.1.0.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bd82612a8f408ad0ed49e08a148e4e814ec3be09015d8c9f0aa1ec8d13ef24b5
|
|
BLAKE2b-256 checksum How to use checksums |
5cfa05c9d9b62ebe7d67375d333b56bda0dc02f7fcd9b2bf3537db794c248a44
|
| 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 18, 2026.
Transparency logRelease files / mrz_checkdigit-0.1.0-py3-none-any.whl
| Download URL | mrz_checkdigit-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
056b79dc953d365e1cce872125d1f244fa4664942807ee9be71ba9e559ad12a2
|
|
BLAKE2b-256 checksum How to use checksums |
b66d134fc67754e579c5a31c146fa03afe7b12b7e3ed25ef7e525be1da4cd0bd
|
| 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 18, 2026.
Transparency logSecurity for mrz-checkdigit 0.1.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.1.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 18, 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.