Added imeigen.awk

This commit is contained in:
Luxferre
2024-03-12 09:57:02 +02:00
parent 0182ea8f52
commit e92f20e471
3 changed files with 32 additions and 2 deletions
+30
View File
@@ -0,0 +1,30 @@
# Valid IMEI generator script
#
# Usage: awk -f imeigen.awk [TAC]
# you can pass any amount of digits, if less than 15 then the rest
# is randomized and the checksum is calculated accordingly
# if more, then it's truncated to 14 and the checksum is calculated
# if no TAC is passed, a fully random valid IMEI is generated
#
# Created by Luxferre in 2024, released into public domain
# Luhn checksum (picoLuhn variation for 14 digits)
function luhn(imeistr, acc, i, l, d) {
acc = 0
l = length(imeistr) # must be even in this variation
for(i=0;i<l;i++) {
d = int(substr(imeistr, i+1, 1)) # get current digit
acc = (acc + int(d * ((i%2) == 1 ? 2.2 : 1))) % 10 # accumulate
}
return (10 - acc) % 10 # finalize the check digit
}
BEGIN {
srand() # init PRNG
imei=ARGV[1]
gsub(/[^0-9]/, "", imei) # remove all non-digits
for(i=0;i<14;i++) # append 14 random digits
imei = imei int(rand()*10)
imei = substr(imei, 1, 14) # truncate to first 14 digits
print(imei luhn(imei)) # output complete IMEI with checksum
}