Luhn algorithm
From Wikipedia, the free encyclopedia
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 and Canadian Social Insurance Numbers. It was created by IBM scientist Hans Peter Luhn and described in US Patent 2,950,048, filed on January 6, 1954 and granted on August 23, 1960.
The algorithm is in the public domain and is in wide use today. It is not intended to be a cryptographically secure hash function; it was designed to protect against accidental errors, not malicious attacks. Most credit cards and many government identification numbers use the algorithm as a simple method of distinguishing valid numbers from collections of random digits.
Contents |
[edit] Informal explanation
The formula verifies a number against its included check digit, which is usually appended to a partial account number to generate the full account number. This account number must pass the following test:
- Starting with the rightmost digit (which is the check digit) and moving left, double the value of every second digit. For any digits that thus become 10 or more, add their digits together as if casting out nines. For example, 1111 becomes 2121, while 8763 becomes 7733 (from 2×6=12 → 1+2=3 and 2×8=16 → 1+6=7).
- Add all these digits together. For example, if 1111 becomes 2121, then 2+1+2+1 is 6; and 8763 becomes 7733, so 7+7+3+3 is 20.
- If the total ends in 0 (put another way, if the total modulus 10 is congruent to 0), then the number is valid according to the Luhn formula; else it is not valid. So, 1111 is not valid (as shown above, it comes out to 6), while 8763 is valid (as shown above, it comes out to 20).
[edit] Example
Consider the example identification number 446-667-651. The first step is to double every other digit, starting with the second-to-last digit and moving left, and sum the digits in the result. The following table shows this step (highlighted rows indicating doubled digits):
Digit | Double | Reduce | Sum of digits |
---|---|---|---|
1 | 1 | 1 | |
5 | 10 | 1+0 | 1 |
6 | 6 | 6 | |
7 | 14 | 1+4 | 5 |
6 | 6 | 6 | |
6 | 12 | 1+2 | 3 |
6 | 6 | 6 | |
4 | 8 | 0+8 | 8 |
4 | 4 | 4 | |
Total Sum: | 40 |
The sum of 40 is divided by 10; the remainder is 0, so the number is valid.
[edit] Implementation
This PHP 5 function implements the algorithm described above, returning true
if the given array of digits represents a valid Luhn number, and false
otherwise.
function checkLuhn($cardNumber) { // Copyright (c) Richard Warrender. Licenced under the GFDL. // http://www.vividreflection.com/ // Get total amount of digits to process $digitCount = strlen((String) $cardNumber); // Checksum must be zero to begin with $checksum = 0; // Loop round card number extracting digits for($i = 1; $i<=$digitCount; $i++) { // Extract digit number $digits[$i] = (int) substr($cardNumber, -$i, 1); // Check to see if this the luhn number, we need to double it if(($i%2) == 0) { // Double luhn digit $digit = $digits[$i] * 2; // If greater or equal to 10, then use sum of digits if($digit >= 10) { // Get first digit $firstDigit = substr($digit, 0, 1); // Get second digit $secondDigit = substr($digit, 1, 1); /// Add together and replace original luhn digit $digit = $firstDigit + $secondDigit; } // Reload back into array $digits[$i] = $digit; } // Keep a running total for use in checksum $checksum += $digits[$i]; } if(($checksum % 10) == 0) { return true; } else { return false; } }
This C# function implements the algorithm described above, returning true
if the given array of digits represents a valid Luhn number, and false
otherwise.
bool CheckNumber(int[] digits) { int sum = 0; bool alt = false; for(int i = digits.Length - 1; i >= 0; i--) { if(alt) { digits[i] *= 2; if(digits[i] > 9) { digits[i] -= 9; // equivalent to adding the digits of value } } sum += digits[i]; alt = !alt; } return sum % 10 == 0; }
The following is an algorithm (in C#) to generate a number that passes the Luhn algorithm. It fills an array with random digits, then computes the sum of those numbers as shown above and places the difference 10-sum
(modulo 10) in the last element of the array.
int[] CreateNumber(int length) { Random random = new Random(); int[] digits = new int[length]; for(int i = 0; i < length - 1; i++) { digits[i] = random.Next(10); } int sum = 0; bool alt = true; for(int i = length - 2; i >= 0; i--) { if(alt) { int temp = digits[i]; temp *= 2; if(temp > 9) { temp -= 9; } sum += temp; } else { sum += digits[i]; } alt = !alt; } int modulo = sum % 10; if(modulo > 0) { digits[length-1] = 10 - modulo; } return digits; }
[edit] Strengths and weaknesses
The Luhn algorithm will detect any single-digit error, as well as almost all transpositions of adjacent digits. It will not, however, detect transposition of the two-digit sequence 09 to 90 (or vice versa). Other, more complex check-digit algorithms (such as the Verhoeff algorithm) can detect more transcription errors. The Luhn mod N algorithm is an extension that supports non-numerical strings.
The algorithm appeared in a US Patent for a hand-held, mechanical device for computing the checksum. It was therefore required to be rather simple. The device took the mod 10 sum by mechanical means. The substitution digits, that is, the results of the double and reduce procedure, were not produced mechanically. Rather, the digits were marked in their permuted order on the body of the machine.
[edit] External links
- C# Implementation -- Tested
- ColdFusion implementation - Works (tested)
- Java implementation
- Java implementation - Includes test case
- Perl implementation for credit cards - Works (tested)
- Perl implementation of LUHN checking - Works (tested)
- PHP implementation
- Python implementation
- Python library
- Ruby LUHN Implementation and card type check
- VB.NET implementation
- T-SQL implementation
- US Patent 2,950,048 Computer for Verifying Numbers, Hans P. Luhn, August 23, 1960.