Added luhn algorithm example WITH THE OL MODULO 10

This commit is contained in:
Dionysus 2024-05-12 03:44:49 -04:00
parent 6d57a8552a
commit f3a54fc25c
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE
2 changed files with 57 additions and 11 deletions

View File

@ -100,17 +100,22 @@ Most cards do not even use a 3rd track, and is mostly un-used worldwide.
### Issuer Identification Number *(IIN)* ### Issuer Identification Number *(IIN)*
| Issuer | INN Range | PAN Digits | | Issuer | INN Range | PAN Digits |
| ------------------------- | ------------------------------- | ---------- | | ------------------------- | -------------------------------- | ---------- |
| American Express | 34-37 | 15 | | American Express | 34-37 | 15 |
| China Union Pay | 62 | 16-19 | | China Union Pay | 62 | 16-19 |
| Diners Club International | 36, 38, 39, 309, 300-3005 | 14 | | Diners Club International | 36, 38, 39, 309, 300-3005 | 14 |
| Discover | 65, 644-649, 6011 622126-622925 | 16, 19 | | Discover | 65, 644-649, 6011, 622126-622925 | 16, 19 |
| JCB | 3528-3589 | 16 | | JCB | 3528-3589 | 16 |
| Maestro | 50, 56-69 | 12-19 | | Maestro | 50, 56-69 | 12-19 |
| Mastercard | 51-55, 2221-2720 | 16 | | Mastercard | 51-55, 2221-2720 | 16 |
| RuPay | 607 | 16 | | RuPay | 607 | 16 |
| Visa | 4 | 12, 16, 19 | | Visa | 4 | 12, 16, 19 |
## Notes
The [Luhn Algorithm](https://en.wikipedia.org/wiki/Luhn_algorithm) or modulo-10 Algorithm *([example python code](./luhn_algo.py))* is used to determine the last digit of the account number, also known as the Check Number.
This number should be divisible by 10, hence "modulo-10" ;)
## References ## References
- [Digital Card](https://en.wikipedia.org/wiki/Digital_card) - [Digital Card](https://en.wikipedia.org/wiki/Digital_card)
- [Magnetic Stripe Card Standards](https://www.magtek.com/content/documentationfiles/d99800004.pdf) - [Magnetic Stripe Card Standards](https://www.magtek.com/content/documentationfiles/d99800004.pdf)

41
luhn_algo.py Normal file
View File

@ -0,0 +1,41 @@
#!/usr/bin/env python
# Luhn Algorithm Example - Developed by acidvegas in Python (https://git.acid.vegas/msr90)
def modulus10(card_number: str) -> str:
'''
Validate a card number using the Luhn Algorithm
:param card_number: The card number to validate
'''
digits = [int(d) for d in card_number]
total_sum = 0
reversed_digits = digits[::-1]
for i in range(len(reversed_digits)):
digit = reversed_digits[i]
if i % 2 != 0:
digit = digit * 2
if digit > 9:
digit -= 9
total_sum += digit
check_digit = (10 - (total_sum % 10)) % 10
full_card_number = card_number + str(check_digit)
if (total_sum + check_digit) % 10 != 0:
raise ValueError('failed luhn check (non-divisible by 10)')
return full_card_number
if __name__ == '__main__':
card_number = input('Enter your card number without the last digit:')
if not card_number.isdigit():
raise ValueError('invalid card number')
print(f'Full card number: {modulus10(card_number)}')