Skip to main content

Generalized Luhn algorithm to any numerical base

Luhn algorithm is described in Annex B of ISO/IEC 7812-1:2017. As it is described at Luhn's Wikipedia page, the algorithm is used to compute check digits and check them in identification numbers, like the usually found in credit card numbers. The standard algorithm uses 10 as numeric base for the check digit.

But there are other scenarios, like nih URIs at IETF RFC 6920, where the numeric base of the check digit to be computed and validated is 16 (see examples at section 8.2 of RFC 6920).

This library is just an implementation of the algorithm for any base. There are two methods, compute and validate, which take as first parameter either an integer, or a string representing the number in the base, or an array of integers, where the value of each position is the corresponding numeric digit. compute method computes the check digit, meanwhile validate takes the whole identification number, the numerical base and the check digit, and answers either True or False.

genluhn.compute('5326-9057-e12f-e2b7-4ba0-7c89-2560-a2',16)
# It return 15

genluhn.validate('5326-9057-e12f-e2b7-4ba0-7c89-2560-a2', 16, 15)
# It returns True

genluhn.validate('5326-9057-e12f-e2b7-4ba0-7c89-2560-a2', 16, 14)
# It returns False

Accessory methods of this library are intToDigits, strToDigits and bytesToDigits, which are used when either an integer, or a string or a byteslike object have to be translated to a list of digits in the given numerical base.

Release files for genluhn 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for genluhn 0.3.1
File Size Uploaded
genluhn-0.3.1.tar.gz 13.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for genluhn 0.3.1
File Interpreter ABI Platform
genluhn-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 27.6 kB

Release files / genluhn-0.3.1.tar.gz

Download URL genluhn-0.3.1.tar.gz
Size 13.6 kB
Tags Source
SHA-256 checksum
How to use checksums
a92a158501a23c9e951e61e227a3bb7952b352ea85e2c3d979b0883c2f6782b5
BLAKE2b-256 checksum
How to use checksums
a869bc22c70131f1d63c08a6f9e6673579d764579764f3d82675a46ca29f3dc6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.16

Release files / genluhn-0.3.1-py3-none-any.whl

Download URL genluhn-0.3.1-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fd7e12e9f0a4cbe12d3a2e153d6363901cf0318eda64fa64122360fb47b4b73b
BLAKE2b-256 checksum
How to use checksums
a7cbc85fab03018d425f4b5ecf6c4d86994ab2fb2cfa6d53cbd3cd0c2536c50a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.16

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page