FACTOID # 33: In 2002, every 1000 Swedes made a bus.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

Encyclopedia > 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 and Canadian Social Insurance Numbers. It was created by IBM scientist Hans Peter Luhn and described in U.S. Patent 2,950,048 , filed on January 6, 1954, and granted on August 23, 1960. Modular arithmetic (sometimes called modulo arithmetic, or clock arithmetic because of its use in the 24-hour clock system) is a system of arithmetic for integers, where numbers wrap around after they reach a certain value — the modulus. ... In mathematics, computing, linguistics, and related disciplines, an algorithm is a finite list of well-defined instructions for accomplishing some task that, given an initial state, will terminate in a defined end-state. ... A checksum is a form of redundancy check, a simple way to protect the integrity of data by detecting errors in data that are sent through space (telecommunications) or time (storage). ... Look up credit card in Wiktionary, the free dictionary. ... A sample SIN card. ... For other uses, see IBM (disambiguation) and Big Blue. ... Hans Peter Luhn (July 1, 1896 in Barmen, Germany – 1964) computer scientist for IBM, creator of the Luhn algorithm and KWIC (Key Words In Context) indexing. ... is the 6th day of the year in the Gregorian calendar. ... Year 1954 (MCMLIV) was a common year (link will display full calendar) of the Gregorian calendar. ... is the 235th day of the year (236th in leap years) in the Gregorian calendar. ... Year 1960 (MCMLX) was a leap year starting on Friday (link will display full calendar) of the Gregorian calendar. ...


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. The public domain comprises the body of all creative works and other knowledge—writing, artwork, music, science, inventions, and others—in which no person or organization has any proprietary interest. ... In cryptography, a cryptographic hash function is a hash function with certain additional security properties to make it suitable for use as a primitive in various information security applications, such as authentication and message integrity. ...

Contents

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:

  1. Counting from 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, subtract 9 from the result. For example, 1111 becomes 2121, while 8763 becomes 7733 (from 2×6=12 → 12-9=3 and 2×8=16 → 16-9=7).
  2. 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.
  3. 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).

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 Verhoeff algorithm, a checksum formula for error detection first published in 1969, was developed by Dutch mathematician Jacobus Verhoeff (born 1927). ... The Luhn mod N algorithm is an extension to the Luhn algorithm (also known as mod 10 algorithm) that allows it to work with sequences of non-numeric characters. ...


Because the algorithm operates on the digits in a right-to-left manner and zero digits only affect the result if they cause shift in position, zero-padding the beginning of a string of numbers does not affect the calculation. Therefore, systems that normalize to a specific number of digits by converting 1234 to 00001234 (for instance) can perform Luhn validation before or after the normalization and achieve the same result.


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.


Example

Consider the example identification number 446-667-651. The first step is to double every other digit, counting from 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 Result
1 1 1
5 10 10-9 1
6 6 6
7 14 14-9 5
6 6 6
6 12 12-9 3
6 6 6
4 8 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.


A lookup table (i.e. calculate Double, Reduce, and Sum of digits only once and for all) can be used (0123456789 is mapped to 0246813579)

Digit Double Reduce Result
0 0 0 0
1 2 2 2
2 4 4 4
3 6 6 6
4 8 8 8
5 10 10-9 1
6 12 12-9 3
7 14 14-9 5
8 16 16-9 7
9 18 18-9 9

Implementation

This C# function implements the algorithm described above, returning true if the given array of digits represents a valid Luhn number, and false otherwise. The title given to this article is incorrect due to technical limitations. ...

 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; } } 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. The title given to this article is incorrect due to technical limitations. ...

 int[] CreateNumber(int length) { Random random = new Random(); int[] digits = new int[length]; // For loop keeps default value of zero for last slot in array 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; } // No else req'd - keep default value of zero for digits[length-1] return digits; } 

Other implementations

References


  Results from FactBites:
 
Luhn algorithm - Wikipedia, the free encyclopedia (580 words)
The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, was developed in the 1960s as a method of validating identification numbers.
It was created in the 1960s by IBM scientist Hans Peter Luhn (1896–1964).
The algorithm is in the public domain and is in wide use today.
Luhn Algorithm : MerchantSite E-commerce guide (137 words)
Luhn Algorithm itself is a simple check and only verifies that the Credit Card number is of a valid format.
Luhn algorithm checks are often made in online systems to verify that the number was correctly entered and as a first line security check.
As the algorithm bares not relation to whether the card is stolen, whether the owner details are correct or whether funds are available it is not suitable to be used as the sole check on card details.
  More results at FactBites »

 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your location
Your comments
Please enter the 5-letter protection code


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.