stdnum.luhn

The Luhn and Luhn mod N algorithms.

The Luhn algorithm is used to detect most accidental errors in various identification numbers.

>>> is_valid('7894')
False
>>> checksum('7894')
6
>>> calc_check_digit('7894')
'9'
>>> is_valid('78949')
True

An alternative alphabet can be provided to use the Luhn mod N algorithm. The default alphabet is ‘0123456789’.

>>> is_valid('1234', alphabet='0123456789abcdef')
False
>>> checksum('1234', alphabet='0123456789abcdef')
14
stdnum.luhn.calc_check_digit(number, alphabet='0123456789')

With the provided number, calculate the extra digit that should be appended to make it pass the Luhn checksum.

stdnum.luhn.checksum(number, alphabet='0123456789')

Calculate the Luhn checksum over the provided number. The checksum is returned as an int. Valid numbers should have a checksum of 0.

stdnum.luhn.is_valid(number, alphabet='0123456789')

Checks to see if the number provided passes the Luhn checksum.

Previous topic

stdnum.iso7064

Next topic

stdnum.verhoeff