stdnum.luhn

The Luhn and Luhn mod N algorithms.

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

>>> validate('7894')
Traceback (most recent call last):
    ...
InvalidChecksum: ...
>>> checksum('7894')
6
>>> calc_check_digit('7894')
'9'
>>> validate('78949')
'78949'
Traceback (most recent call last):
    ...
InvalidChecksum: ...

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

>>> validate('1234', alphabet='0123456789abcdef')
Traceback (most recent call last):
    ...
InvalidChecksum: ...
>>> checksum('1234', alphabet='0123456789abcdef')
14
Traceback (most recent call last):
    ...
InvalidChecksum: ...
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.

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

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

Previous topic

stdnum.iso7064

Next topic

stdnum.verhoeff