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: ...
With the provided number, calculate the extra digit that should be appended to make it pass the Luhn checksum.
Calculate the Luhn checksum over the provided number. The checksum is returned as an int. Valid numbers should have a checksum of 0.
Checks to see if the number provided passes the Luhn checksum.
Checks to see if the number provided passes the Luhn checksum.