mirror of
https://github.com/srlabs/blue-merle.git
synced 2025-01-03 03:20:52 -05:00
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:
parent
512da23200
commit
c8634591b2
61
files/lib/blue-merle/luhn.lua
Normal file
61
files/lib/blue-merle/luhn.lua
Normal 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"))
|
Loading…
Reference in New Issue
Block a user