Implement Luhn algorithm to generate valid credit card numbers with python
Credit card numbers follow certain patterns.
The very first number is the Major Industry Identifier (MII), which tells you what sort of institution issued the card.
- 1 and 2 are issued by airlines
- 3 is issued by travel and entertainment
- 4 and 5 are issued by banking and financial institutions
- 6 is issued by merchandising and banking
- 7 is issued by petroleum companies
- 8 is issued by telecommunications companies
- 9 is issued by national assignment
The first six digits are the Issuer Identification Number (IIN). These can be used to look up where the card originated from.
- Visa: 4*****
- American Express (AMEX): 34**** or 37****
- Mastercard: 51**** to 55****
The seventh digit to the second-to-last digit is the customer account number.We often see 16-digit credit card numbers today, but it’s possible to issue a card with up to 19 digits using the current system.
The very last digit of a credit card is the check digit or checksum. It is used to validate the credit card number using the Luhn algorithm.
Step 1. Generate 15 random numbers based on the above instructions.(first 6 for IIN and 7th to 15th for Account Number).
Step 2. Multiply odd position digits with 2 ( Double every second digit, from the rightmost).
***Prepending a 0 to odd-length numbers makes it possible to process the number from left to right rather than right to left.
Step 3. Deduct 9 from the numbers greater than 9 (or add up the two digits to get a single-digit number (like for 12:1+2, 18=1+8)).
Step 4. Sum all the individual digits (38 +x).
Let’s consider that our bank’s IIN number is 400000.
Then the program to generate credit card number (using python)
You can check the validity of the number using with this credit card validator.
Read my Django Series..