-
Notifications
You must be signed in to change notification settings - Fork 9
/
card_num_generator.py
92 lines (71 loc) · 2.71 KB
/
card_num_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
# author: garbu
from random import randint
def generate_card(type):
"""
Prefill some values based on the card type
"""
card_types = ["americanexpress","visa13", "visa16","mastercard","discover"]
def prefill(t):
# typical number of digits in credit card
def_length = 16
"""
Prefill with initial numbers and return it including the total number of digits
remaining to fill
"""
if t == card_types[0]:
# american express starts with 3 and is 15 digits long
# override the def lengths
return [3, randint(4,7)], 13
elif t == card_types[1] or t == card_types[2]:
# visa starts with 4
if t.endswith("16"):
return [4], def_length - 1
else:
return [4], 12
elif t == card_types[3]:
# master card start with 5 and is 16 digits long
return [5, randint(1,5)], def_length - 2
elif t == card_types[4]:
# discover card starts with 6011 and is 16 digits long
return [6, 0, 1, 1], def_length - 4
else:
# this section probably not even needed here
return [], def_length
def finalize(nums):
"""
Make the current generated list pass the Luhn check by checking and adding
the last digit appropriately bia calculating the check sum
"""
check_sum = 0
#is_even = True if (len(nums) + 1 % 2) == 0 else False
"""
Reason for this check offset is to figure out whther the final list is going
to be even or odd which will affect calculating the check_sum.
This is mainly also to avoid reversing the list back and forth which is specified
on the Luhn algorithm.
"""
check_offset = (len(nums) + 1) % 2
for i, n in enumerate(nums):
if (i + check_offset) % 2 == 0:
n_ = n*2
check_sum += n_ -9 if n_ > 9 else n_
else:
check_sum += n
return nums + [10 - (check_sum % 10) ]
# main body
t = type.lower()
if t not in card_types:
print "Unknown type: '%s'" % type
print "Please pick one of these supported types: %s" % card_types
return
initial, rem = prefill(t)
so_far = initial + [randint(1,9) for x in xrange(rem - 1)]
print "Card type: %s, " % t,
print "".join(map(str,finalize(so_far)))
# run - check
generate_card("discover")
generate_card("mastercard")
generate_card("americanexpress")
generate_card("visa13")
generate_card("visa16")