Credit Card Number Generator
From Leo's Notes
Last edited on 27 January 2014, at 07:40.
I got bored one night and decided to create this as a quick exercise. This is a number generator that satisfies the luhn + MOD 10 algorithm that is used by credit card numbers.
Different issuers/banks will use different prefixes. For instance, Visa will use values 40-49 while MasterCard uses 50-59. A complete list can be found on Wikipedia.
Code
#!/usr/local/bin/python
import sys
from random import randint
def pad(prefix, values):
prefix = str(prefix)
i = 0
for c in prefix:
values[i] = int(c)
i = i + 1
return values
def randomize(values):
for i in range(0, len(values)):
values[i] = randint(0, 9)
return values
def luhn_value(values):
sum = 0
i = 0
while i < len(values):
if i % 2 == 0:
value = values[i] * 2
# Since doubling a single value results in 2 <= i <= 18
# the sum of the digits is equivolant to subtracting 9
if value >= 10:
value = value - 9
else:
value = values[i]
sum = sum + value
i = i + 1
return (sum * 9) % 10
def generate(prefix = ""):
length = 15
values = [0]*length
prefix = str(prefix)
values = randomize(values)
values = pad(prefix, values)
check = luhn_value(values)
return "".join(str(n) for n in values) + str(check)
if len(sys.argv) == 2:
if sys.argv[1].isdigit():
print(generate(sys.argv[1]))
else:
print("Usage: cc.py [prefix]")
else:
print(generate())
Example
python cc.py 45 4539371684142703
python cc.py 55 5555576731917875