Luhn algorithm or Luhn
formula, also known as "modulo 1 algorithm". It is a simple verification formula, which is generally used to verify the ID number, IMEI number, American supplier identification number, or Canadian social security number. The algorithm was created by Hans
Peter Luhn, a scientist from IBM. The patent application was filed on January 6, 1954, and was awarded on August 23, 196. The patent number in the United States is 29548.
this algorithm has always been shared by everyone, and it is also widely used today. It is specified in ISO/IEC7812-1. Its purpose is not to be an encrypted and secure hash function; Its purpose is to prevent unexpected mistakes, not malicious attacks. Many credit cards and many government identification numbers use this algorithm to extract valid numbers from a series of random numbers.
Advantages and Disadvantages
Luhn
Algorithm will detect any single code error and almost all adjacent digit transposition errors. However, it will not detect the error of two digit sequences from 9 to 9 (and vice versa). It will detect seven-tenths of the same double-digit errors (the exchange of 22 and 55, the exchange of 33 and 66, and the exchange of 44 and 77 will not be detected). Other more complex algorithms for checking numbers, such as Fairhoff algorithm, can detect more transcription errors. Luhn algorithm of modulo n is an extension of Luhn algorithm, which supports non-numeric strings. Because the algorithm is from right to left, and the zero position will affect the calculation results. Only when the zero position causes the digits to move or the beginning of a series of digits is filled with zeros will it not affect the generation of calculation results. Therefore, whether before or after filling 1234 with zeros to 1234, the results obtained by using Luhn algorithm are the same.
This algorithm is patented in the United States to calculate check codes for handheld or mechanical devices. So it must be as simple as possible.
Informal explanation
This formula will verify a series of numbers through a check code. The check code is usually added to the account number to form a complete account number. The pieced account number must pass the following tests:
1. Count from the parity bit (the parity bit is usually added at the back of the account), and multiply the even numbers by 2 in the order from right to left.
2. Add up the obtained results (for example, 1=1+=1,14=1+4=5, and some people say that it is multiplied by 2.
3. If the sum of the numbers is after modulo 1 (that is, the result of the addition ends in , which is a multiple of 1), then this number string is valid according to the Luhn algorithm, and vice versa.
suppose a string is "7992739871" and we add a check bit to it. The final number is 7992739871x:
account number: 7 9 9
2 7 3
9 8 7 1 x
the even number is multiplied by 2: 71894769
1672
x
and the number after adding up is 7 9 9 4 7 6 9 7. 7
2 =67
The check code X is calculated by multiplying the added number by 9, and then performing modulo 1 calculation (that is, (67*9)mod1, and some people say that it is an integer multiple of the added number and the smallest 1, but the results are all the same). Generally speaking:
1. Calculate the sum of all digits (67).
2. multiply it by 9(63).
3. Take the last digit (3).
4. the result is the parity bit.
another way to get the parity bit: first calculate the sum of all digits, and subtract the result of all digits and modulo 1 from 1. (The unit of 67 is 7; 1-7=3 is the parity bit). Generally speaking:
1. Calculate the sum of all digits (67).
2. Take a single digit (7).
3. subtract the single digits from 1 (3).
4. the result is the parity bit.
in this way, we got the complete account number: 7992739871x.
each of the following numbers is 7992739871, 79927398711, 79927398713, 79927398714,
7992739875
1. counting from the rightmost, multiply the even digits by 2: (1 * 2) = 2, (8*2)=16, (3*2)=6, (2*2)=4, (9*2)=18
2. add up each digit: It should be noted that 3 is the only single digit that can make the sum (67+x) an integer multiple of 1.
4. Therefore, all the above accounts are invalid except 79927398713.
code realization of check bit verification
the following is realized by Python:
defluhn _ checksum (card _ number):
defdigits _ of (n):
return [int (d) for d in str (n)].
digits = digits_of(card_number)
odd_digits = digits[-1::-2]
even_digits = digits[-2::-2]
checksum =
checksum += sum(odd_digits)
for d in even_digits:
checksum += sum(digits_of(d*2))
return checksum% 1
def is _ luhn _ valid (card _ number):
return luhn _ checksum (card _ number) = =
calculation of parity bit
The above algorithm checks the validity of the input parity bit. Computing parity bits requires a small adaptive algorithm, namely:
1. Switching odd/even multiplication.
2. if the sum modulus 1 is equal to , then the check code is .
3. Otherwise, the check code is equal to the sum of 1 (1-(sum mod 1))
def calculate _ luhn (partial _ card _ number):
return 1-luhn _ checksum (int (partial _ card _ number) * 1).