Skip to content

Latest commit

 

History

History
46 lines (32 loc) · 1.2 KB

README.md

File metadata and controls

46 lines (32 loc) · 1.2 KB

Luhn algorithm

The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers in US and Canadian Social Insurance Numbers.

Informations

  • First published - 1954
  • Digest sizes - 4 bits
  • Structure - sum

Authors

Hans Peter Luhn

Implementations

  • luhn.c

Materials

Pseudocode

The implementations below are in Python.

def luhn_checksum(card_number):
    def digits_of(n):
        return [int(d) for d in str(n)]
    digits = digits_of(card_number)
    odd_digits = digits[-1::-2]
    even_digits = digits[-2::-2]
    checksum = 0
    checksum += sum(odd_digits)
    for d in even_digits:
        checksum += sum(digits_of(d*2))
    return checksum % 10

def calculate_luhn(partial_card_number):
    check_digit = luhn_checksum(int(partial_card_number) * 10)
    return check_digit if check_digit == 0 else 10 - check_digit

def is_luhn_valid(card_number):
    return luhn_checksum(card_number) == 0