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, IMEI numbers, National Provider Identifier numbers in US and Canadian Social Insurance Numbers. It was created by IBM scientist Hans Peter Luhn and described in U.S. Patent No. 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 specified in ISO/IEC 7812-1[1]. 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
1 Strengths and weaknesses
2 Informal explanation
3 Mod 10+5 Variant
4 Implementation of standard Mod 10
5 Other implementations
6 References
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). It will detect 7 of the 10 possible twin errors (it will not detect 22 ↔ 55, 33 ↔ 66 or 44 ↔ 77).
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.
Because the algorithm operates on the digits in a right-to-left manner and zero digits affect the result only 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.
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:
Counting from the check digit, which is the rightmost, and moving left, double the value of every second digit.
Sum the digits of the products together with the undoubled digits from the original number.
If the total ends in 0 (put another way, if the total modulo 10 is equal to 0), then the number is valid according to the Luhn formula; else it is not valid.
As an illustration, if the account number is 49927398716, it will be validated as follows:
Double every second digit, from the rightmost: (1×2) = 2, (8×2) = 16, (3×2) = 6, (2×2) = 4, (9×2) = 18
Sum all the individual digits (digits in parentheses are the products from Step 1): 6 + (2) + 7 + (1+6) + 9 + (6) + 7 + (4) + 9 + (1+8) + 4 = 70
Take the sum modulo 10: 70 mod 10 = 0; the account number is valid.
Mod 10+5 Variant
Some credit cards use the "Mod 10 plus 5" variant to extend the space of valid card numbers.[citation needed] In this variant, if the sum ends in 0 or 5, the number is considered valid.
Implementation of standard Mod 10
Python variant:
def is_mod10(cc):
dub, tot = 0, 0
for i in range(len(cc) - 1, -1, -1):
for c in str((dub + 1) * int(cc[i])):
tot += int(c)
dub = (dub + 1) % 2
return (tot % 10) == 0
Java variant:
public static boolean isValidCC(String num) {
final int[][] sumTable = {{0,1,2,3,4,5,6,7,8,9},{0,2,4,6,8,1,3,5,7,9}};
int sum = 0, flip = 0;
for (int i = num.length() - 1; i >= 0; i--, flip++)
sum += sumTable[flip & 0x1][num.charAt(i) - '0'];
return sum % 10 == 0;
}
c variant:
int LuhnMod10 ( char * cardNumber , int size )
{
static int table [ 10 ] = { { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } , { 0 , 2 , 4 , 6 , 8 , 1 , 3 , 5 , 7 , 9 } } ;
for ( int i = size - 1 , odd = 0 , sum = 0 ; i >= 0 ; i -- )
if ( isdigit ( cardNumber [ i ] ) )
sum += table [ ( odd = 1 - odd ) ] [ cardNumber [i] - '0' ] ;
sum %= 10 ;
return ( sum ? 10 - sum : 0 ) ; /* return the check digit */
}
这里有一个表格,概述了主要的信用卡,您可能要验证。
Here is a table outlining the major credit cards that you might want to validate.
卡类型CARD TYPE
前缀Prefix
长度Length
校验码算法Check digit algorithm
万事达卡MASTERCARD
51-5551-55
1616
10mod 10
签证VISA
44
13,1613, 16
10mod 10
美国证券交易所AMEX
3434
37 37
1515
10mod 10
大来卡/Diners Club/全权委托Carte Blanche
300-305300-305
36 36
38 38
1414
10mod 10
探索Discover
60116011
1616
10mod 10
途中enRoute
2014年2014
2149 2149
1515
任何any
联合协调JCB
33
1616
10mod 10
联合协调JCB
21312131
1800 1800
1515
10mod 10