repl.it
@joelrf/

Generate IMEIs based on TAC code

Python 2.7

No description

fork
loading
Files
  • main.py
main.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
#!/usr/bin/env python2
import random


def checksum(string):
    digits = list(map(int, string))
    odd_sum = sum(digits[-1::-2])
    even_sum = sum([sum(divmod(2 * d, 10)) for d in digits[-2::-2]])
    return (odd_sum + even_sum) % 10


def generate(string):
    cksum = checksum(string + '0')
    return (10 - cksum) % 10


def append_luhn(string):
    return string + str(generate(string))


def generate_imei(subject_imei):
    imei_digits = []

    for i in xrange(0, 14):
        # find missing digits and complete with randos
        try:
            digit = subject_imei[i]
        except IndexError:
            digit = random.randint(0, 9)

        # Add digits to IMEI
        imei_digits.append(str(digit))

    # append the luhn checksum and return
    return append_luhn("".join(imei_digits))


def generate_list(tac_code):

    print("TAC:  %s" % tac_code)
    print("IMEIs:")

    for x in xrange(0, 20):
        print(generate_imei(tac_code))


while True:
    try:
        tac = raw_input('Enter TAC code:')
        generate_list(tac)
        print "---------------------"
    except ValueError:
        print "Not a valid TAC code"
?