From c8634591b2c4956e5aed6247c761caab4e4cd7c0 Mon Sep 17 00:00:00 2001 From: Tobias Mueller Date: Wed, 18 Oct 2023 10:31:49 +0200 Subject: [PATCH] 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. --- files/lib/blue-merle/luhn.lua | 61 +++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 files/lib/blue-merle/luhn.lua diff --git a/files/lib/blue-merle/luhn.lua b/files/lib/blue-merle/luhn.lua new file mode 100644 index 0000000..6f1b1dc --- /dev/null +++ b/files/lib/blue-merle/luhn.lua @@ -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"))