Skip to main content

Generalized Luhn algorithm to any numeric base

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

genluhn-0.3.1.tar.gz (13.6 kB view hashes)

Uploaded Source

Built Distribution

genluhn-0.3.1-py3-none-any.whl (14.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page