Luhn algorithm

The Luhn algorithm (also known as Mod 10) is a simple checksum algorithm that is commonly used to verify identification numbers, such as the card issuing bank identification number, international Mobile Device Identification Number (IMEI), US national provider identification number, or Canadian Social Security number. The algorithm was created by IBM scientist Hans Peter Luhn. The patent was filed on January 6, 1954 and issued on August 23, 1960. The US patent number is 2950048.

The algorithm is now in the public domain and is widely used, such as IN ISO/IEC 7812-1. It is not a secure cryptographic hash function and is designed only to prevent accidental errors rather than malicious attacks

describe

Luhn’s algorithm verifies a string of numbers using a checksum code, which is usually added to the end of the string to produce a complete identification number.

Take the number “7992739871” as an example to calculate its check bit:

  1. Starting from the check digit, right to left, even digit times2(e.g.,1 * 2 = 2),If the product is two digits, then the two-digitbitswithtenAdd (for example,16:1 +6=7, 18:1 +8=9);
  2. Add the resulting numbers together (67 in this case);
  3. The sum of the numbers is modulo 10 (7 in this case) and subtracted by 10 (3 in this case) to get the check bit.
The original number 7 9 9 2 7 3 9 8 7 1 x
Even digits times 2 7 18 9 4 7 6 9 16 7 2 x
Add up the numbers 7 9 9 4 7 6 9 7 7 2 x

implementation

From the above description, it can be known that the steps to implement Luhn algorithm are as follows:

  • 1. Number the card number from right to left1The second from the far right is2The third from the far right is3
  • 2. Traverse from right to left, for each charactertPerform the third step and add up the results of each digit to get a numbers
  • 3. Calculation rule for each digit: If this digit isAn odd number of bits, the returntPer se, if it isThe coded, it will be firsttMultiplied by the2Get a numbernIf thenIs one digit (Less than 10), return directlyn, otherwise willntheSingle digitsandTen digitsAdd back
  • 4, ifsCan be divided exactly by10, the number is valid. Otherwise, the number is invalid

Js implementation

function testPaycard(payCard) {
    / / the number
    if (!/(^\d{16}$)|(^\d{19}$)/.test(payCard)) {
        return false
    }
    const everyNum = String(payCard).split(' '); // Split all the numbers of the bank card
    everyNum.forEach((num, index) = > {
        if (index % 2= = =0) { // Even digits from right to left
            num = Number(num).toString(2) + '0'; // Convert to binary and move binary one bit to the left
            num = parseInt(num, 2); // Revert to decimal
            num = (num / 10 | 0) + num % 10; // The sum of the current digit bitseveryNum[index] = num; }});// Whether the total % 10 is divisible
    return everyNum.reduce((tol, num) = > tol + parseInt(num), 0) % 10= = =0; 
}
Copy the code

validation

testPaycard(6226094425653290)
// true
testPaycard(6226097143099498)
// true
testPaycard(6226094043514957)
// true
Copy the code

Ps: You can try it with your bank card, but there are special banks that don’t use this algorithm.

Reference links: baike.baidu.com/item/Luhn%E…