awk-gold-collection

My best software created for POSIX AWK
git clone git://git.luxferre.top/awk-gold-collection.git
Log | Files | Refs | Submodules | README

imeigen.awk (1044B)


      1 # Valid IMEI generator script
      2 #
      3 # Usage: awk -f imeigen.awk [TAC]
      4 # you can pass any amount of digits, if less than 15 then the rest
      5 # is randomized and the checksum is calculated accordingly
      6 # if more, then it's truncated to 14 and the checksum is calculated 
      7 # if no TAC is passed, a fully random valid IMEI is generated
      8 #
      9 # Created by Luxferre in 2024, released into public domain
     10 
     11 # Luhn checksum (picoLuhn variation for 14 digits)
     12 function luhn(imeistr,  acc, i, l, d) {
     13   acc = 0
     14   l = length(imeistr) # must be even in this variation
     15   for(i=0;i<l;i++) {
     16     d = int(substr(imeistr, i+1, 1)) # get current digit
     17     acc = (acc + int(d * ((i%2) == 1 ? 2.2 : 1))) % 10 # accumulate
     18   }
     19   return (10 - acc) % 10 # finalize the check digit
     20 }
     21 
     22 BEGIN {
     23   srand() # init PRNG
     24   imei = ARGV[1]
     25   gsub(/[^0-9]/, "", imei) # remove all non-digits
     26   for(i=0;i<14;i++) # append 14 random digits
     27     imei = imei int(rand()*10)
     28   imei = substr(imei, 1, 14) # truncate to first 14 digits
     29   print(imei luhn(imei)) # output complete IMEI with checksum
     30 }