Skip to content

Instantly share code, notes, and snippets.

@kamermans
Last active June 8, 2025 19:36
Show Gist options
  • Select an option

  • Save kamermans/9f6fd1f575a11a18a9a970a17b7908ea to your computer and use it in GitHub Desktop.

Select an option

Save kamermans/9f6fd1f575a11a18a9a970a17b7908ea to your computer and use it in GitHub Desktop.
Generates a complete, valid IMEI from a TAC code or otherwise incomplete IMEI.
#!/usr/bin/env python2
from random import randint
def generate_imei(incomplete_imei):
luhn_sum = 0
imei_digits = []
for i in xrange(0,14):
# Pull each digit from the TAC, generate missing numbers with rand()
try:
digit = incomplete_imei[i]
except IndexError:
digit = randint(0, 9)
# Add digits to IMEI
imei_digits.append(str(digit))
# Double every odd indexed digit
if (i % 2 != 0):
digit = digit * 2
# Split digits and add, ex: "14" becomes "1+4"
for component in str(digit):
luhn_sum = luhn_sum + int(component)
remainder = luhn_sum % 10
check_digit = 0 if (remainder == 0) else (10 - remainder)
imei_digits.append(str(check_digit))
return "".join(imei_digits)
#tac = "10457816123456"
#tac = "49015420323751"
tac = "104578"
imei = generate_imei(tac)
print("TAC: %s" % tac)
print("IMEI: %s" % imei)
@adebayo0437
Copy link

35455037

@fnicx
Copy link

fnicx commented Nov 2, 2024

35222509

@ogamax-blip
Copy link

35550510

@kamermans
Copy link
Author

Why is everyone posting TAC codes here? 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment