Method Deck / other

Luhn check digit

Validates a number against the Luhn checksum, or computes the Luhn check digit to append to a number.

Published for review. The Founder is checking every card himself; anything he strikes comes down the same hour.

Try it

Inputs

Outputs

Steps

  1. The Luhn algorithm (used for card numbers per ISO/IEC 7812-1): starting from the rightmost digit of the full number (the check digit) and moving left, double every second digit; if doubling produces a number greater than 9, subtract 9 from it.
  2. Sum all the digits (doubled ones after the above adjustment, others unchanged).
  3. The number is valid if that sum is a multiple of 10.
  4. To compute a missing check digit: try each digit 0-9 appended to the given number until the resulting full number's Luhn sum is a multiple of 10; that digit is the check digit.

Constants

None.

Sources

Worked example and test vectors

NameInputsAnswer
worked example{"mode":"validate","number":"4111111111111111"}true
test vector: compute check digit{"mode":"compute_check_digit","number":"7992739871"}3

Published card