genluhn

Generalized Luhn algorithm to any numeric base


Keywords
library, python
License
LGPL-2.1
Install
pip install genluhn==0.3.0

Documentation

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.