Bank card verification rules

Bank card number, bits 13-19 are generally composed of a "card issuer from the BIN + + parity bit positioned" three parts, wherein

1. The first 6 digits are the card BIN, which is assigned by the International Organization for Standardization to the bank card organization engaged in inter-bank transfer and exchange;

2. The card issuer's custom bit is composed of 6-12 digits and is assigned by the card issuer;

3. The last digit of the card number is calculated according to the Luhn equation based on the digits before the check digit (including BIN).

4. Luhn algorithm for verifying bank cards. In the verification process, the sum is calculated from the rightmost number (number 1), if the number is odd, add that number;

     If it is an even number, the number is multiplied by the middle value of 2, if the middle value does not exceed 9, add the middle value, otherwise the middle value minus 9, plus the middle value.

     If the sum is divisible by 10, the check passes, otherwise the check fails.

5. How to calculate the check digit.

     If the card number B(n)B(n-1)...B(2)B(1)

     W(B(i)) = (2-i%2)*B(i);

     S(B(i)) = (W(B(i)) < 10) ? W(B(i)) : (W(B(i)) - 9);

     S = S(B(1)) + S(B(2)) + ... + S(B(n)) = S(B(1)) + R = B(1) + R;

     Since S is divisible by 10, B(1) = S-R => B(1) = B(1)% 10 = (SR)%10 = -R% 10;

     And B(1) >= 0 && B(1) <= 9; B(1) is unique.

Guess you like

Origin blog.csdn.net/beebeeyoung/article/details/107373478