2007-10-20 21:40:20 +08:00
|
|
|
"""
|
2012-12-25 06:10:40 +08:00
|
|
|
Common checksum routines.
|
2007-10-20 21:40:20 +08:00
|
|
|
"""
|
|
|
|
|
2013-10-27 01:50:40 +08:00
|
|
|
__all__ = ['luhn']
|
2007-10-20 21:40:20 +08:00
|
|
|
|
2012-07-20 20:22:00 +08:00
|
|
|
from django.utils import six
|
|
|
|
|
2013-11-03 03:27:47 +08:00
|
|
|
LUHN_ODD_LOOKUP = (0, 2, 4, 6, 8, 1, 3, 5, 7, 9) # sum_of_digits(index * 2)
|
2007-10-20 21:40:20 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2007-10-20 21:40:20 +08:00
|
|
|
def luhn(candidate):
|
|
|
|
"""
|
|
|
|
Checks a candidate number for validity according to the Luhn
|
|
|
|
algorithm (used in validation of, for example, credit cards).
|
|
|
|
Both numeric and string candidates are accepted.
|
|
|
|
"""
|
2012-07-20 20:22:00 +08:00
|
|
|
if not isinstance(candidate, six.string_types):
|
2007-10-20 21:40:20 +08:00
|
|
|
candidate = str(candidate)
|
|
|
|
try:
|
2013-08-30 07:20:00 +08:00
|
|
|
evens = sum(int(c) for c in candidate[-1::-2])
|
|
|
|
odds = sum(LUHN_ODD_LOOKUP[int(c)] for c in candidate[-2::-2])
|
2007-10-20 21:40:20 +08:00
|
|
|
return ((evens + odds) % 10 == 0)
|
|
|
|
except ValueError: # Raised if an int conversion fails
|
|
|
|
return False
|