Check Digit

The final digit of your credit card number is a check digit, akin to a checksum. The algorithm used to arrive at the proper check digit is called the Luhn algorithm, after IBM scientist Hans Peter Luhn (1896-1964), who was awarded US Patent 2950048 ("Computer for Verifying Numbers") for the technique in 1960. For details about Luhn's life, see

Thanks to Aleksandar Janicijevic for directing me to information about H.P. Luhn.

The most succinct description of the Luhn algorithm I have found comes from the hacker publication phrack 47-8: "For a card with an even number of digits, double every odd numbered digit and subtract 9 if the product is greater than 9. Add up all the even digits as well as the doubled-odd digits, and the result must be a multiple of 10 or it's not a valid card. If the card has an odd number of digits, perform the same addition doubling the even numbered digits instead."

The bit about even and odd is a little confusing. The main point is that you don't want to double the check digit, and this can easily be done by starting with the check digit, going backwards, and doubling every other digit. See the source code below for details.

Examples

These examples are drawn from junk mail I received from credit card issuers in August 2001. Some of this junk mail contained glossy pictures of credit cards, and the sample numbers come directly from two of these pictures.

4408 0412 3456 7890

The first credit card offer showed a picture of a card with the number 4408 0412 3456 7890.

The Major Industry Identifier (MII) is 4 (banking and financial), the issuer identifier is 440804 (a VISA partner), the account number is 123456789, and the check digit is 0.

Let's apply the Luhn check to 4408 0412 3456 7890. In the following table,

  • The top row is the original number.
  • In the second row, we multiply alternate digits by 2. Don't multiply the check digit by 2.
  • In the third row, we force all digits to be less than 10, by subtracting 9 where necessary.
  • The bottom row contains the digits to be added together.
4 4 0 8 0 4 1 2 3 4 5 6 7 8 9 0
4 x 2 = 8 4 0 x 2 = 0 8 0 x 2 = 0 4 1 x 2 = 2 2 3 x 2 = 6 4 5 x 2 = 10 6 7 x 2 = 14 8 9 x 2 = 18 0
8 4 0 8 0 4 2 2 6 4 10 - 9 = 1 6 14 - 9 = 5 8 18 - 9 = 9 0
8 4 0 8 0 4 2 2 6 4 1 6 5 8 9 0

If we add all of the digits in the bottom row together, we get 67, which is not a multiple of 10, and therefore we conclude that the number 4408 0412 3456 7890 is an invalid credit card number.

By changing the check digit from 0 to 3, we arrive at the number 4408 0412 3456 7893, which does pass the Luhn check, since the sum of the digits in the bottom row would be 70, which is divisible by 10. 4408 0412 3456 7893 is, on the face of it, a valid credit card number.

4417 1234 5678 9112

The second credit card offer showed a picture of a card with the number 4417 1234 5678 9112.

The Major Industry Identifier (MII) is 4 (banking and financial), the issuer identifier is 441712 (a VISA partner), the account number is 345678911, and the check digit is 2.

Let's apply the Luhn check to 4417 1234 5678 9112, as we did in the previous example.

4 4 1 7 1 2 3 4 5 6 7 8 9 1 1 2
4 x 2 = 8 4 1 x 2 = 2 7 1 x 2 = 2 2 3 x 2 = 6 4 5 x 2 = 10 6 7 x 2 = 14 8 9 x 2 = 18 1 1 x 2 = 2 2
8 4 2 7 2 2 6 4 10 - 9 = 1 6 14 - 9 = 5 8 18 - 9 = 9 1 2 2
8 4 2 7 2 2 6 4 1 6 5 8 9 1 2 2

If we add all of the digits in the bottom row together, we get 69, which is not a multiple of 10, and therefore we conclude that the number 4417 1234 5678 9112 is an invalid credit card number.

By changing the check digit from 2 to 3, we arrive at the number 4417 1234 5678 9113, which does pass the Luhn check, since the sum of the digits in the bottom row would be 70, which is divisible by 10. 4417 1234 5678 9113 is, on the face of it, a valid credit card number.

Warning

These two credit card offers contained pictures with numbers which the Luhn check proved to be invalid. A change to their check digits made them ostensibly valid. But if I were you, I wouldn't try to charge anything with them.