-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathimei.py
107 lines (83 loc) · 3.21 KB
/
imei.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""
Generates random IMEI numbers.
The user specifies the 8-digit TAC and up to 4-digits of the serial number.
The user also specifies the number of random IMEIs to generate.
"""
import sys
import random
# Src: https://github.com/arthurdejong/python-stdnum/blob/master/stdnum/luhn.py
def checksum(number, alphabet='0123456789'):
"""
Calculate the Luhn checksum over the provided number.
The checksum is returned as an int.
Valid numbers should have a checksum of 0.
"""
n = len(alphabet)
number = tuple(alphabet.index(i)
for i in reversed(str(number)))
return (sum(number[::2]) +
sum(sum(divmod(i * 2, n))
for i in number[1::2])) % n
def calc_check_digit(number, alphabet='0123456789'):
"""Calculate the extra digit."""
check_digit = checksum(number + alphabet[0])
return alphabet[-check_digit]
def main():
"""Ask for the base IMEI, how many to generate, then generate them."""
# Loop until the first 8-12 digits have been received & are valid
start = ''
while True:
try:
start = str(input('Enter the first 8 - 12 digits: ')).strip()
except KeyboardInterrupt:
print('')
sys.exit()
# If all conditions are met, the input is valid
if start.isdigit() and len(start) >= 8 and len(start) <= 12:
break
# Tell the user why their input is invalid
if not start.isdigit():
print('*** Invalid input: you must enter digits only\n')
elif len(start) <= 8:
print('*** Invalid input: you must enter at least 8 digits\n')
elif len(start) >= 12:
print('*** Invalid input: you must enter no more than 12 digits\n')
# Loop until we know how many random numbers to generate
count = 0
while True:
try:
count_input = str(
input('Enter the number of IMEI numbers to generate: ')
).strip()
except KeyboardInterrupt:
print('')
sys.exit()
# If all conditions are met, the input is valid
if count_input.isdigit() and int(count_input) > 0:
count = int(count_input)
break
# Tell the user that they need to enter a number > 0
print('*** Invalid input: you must enter a number greater than zero\n')
# IMEIs will be generated based on the first 8 digits (TAC; the number
# used to identify the model) and the next 2-6 digits (partial serial #).
# The final, 15th digit, is the Luhn algorithm check digit.
# Generate and print random IMEI numbers
print('')
for _ in range(count):
imei = start
# Randomly compute the remaining serial number digits
while len(imei) < 14:
imei += str(random.randint(0, 9))
# Calculate the check digit with the Luhn algorithm
imei += calc_check_digit(imei)
print(imei)
print('')
# Backwards compatibility (raw_input was renamed to input in Python 3.x)
try:
# Using Python 2.x; calls to input will be treated as calls to raw_input
input = raw_input
except NameError:
# Using Python 3.x; no action required
pass
if __name__ == '__main__':
main()