added Lua script to generate an IMEI

We currently use a Python script to generate IMEIs. Loading Python is
relatively expensive on our target platform so I hope we can use
something quicker.
This commit is contained in:
Tobias Mueller 2023-10-18 10:31:49 +02:00
parent 512da23200
commit c8634591b2
1 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,61 @@
---- Adapted from https://cybersecurity.att.com/blogs/labs-research/luhn-checksum-algorithm-lua-implementation
local bit = require("bit")
local band, bor, bxor = bit.band, bit.bor, bit.bxor
function luhn_checksum(card)
local num = 0
local nDigits = card:len()
odd = band(nDigits, 1)
for count = 0,nDigits-1 do
digit = tonumber(string.sub(card, count+1,count+1))
if (bxor(band(count, 1),odd)) == 0
then
digit = digit * 2
end
if digit > 9 then
digit = digit - 9
end
num = num + digit
end
return num
end
function luhn_digit (s)
local num = luhn_checksum (s)
return (10 - (num % 10))
end
function is_valid_luhn (s)
local num = luhn_checksum (s)
return ((num % 10) == 0)
end
function make_imei (premei)
local imei = premei .. tostring(luhn_digit(premei .. "0"))
if is_valid_luhn (imei) then print ("Valid " .. imei) end
return imei
end
function make_random_imei ()
local nDigits = 13
local premei = ""
for count = 0, nDigits-1 do
premei = premei .. tostring (math.random (0, 9))
end
local imei = make_imei (tostring (premei))
return imei
end
print (make_imei ("354809108035177"))