Created
August 24, 2018 05:00
-
-
Save lpcdma/3e3009459f4a3ef7f63f113b60d7ca8e to your computer and use it in GitHub Desktop.
lua genimei function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function serialize(obj) | |
local lua = "" | |
local t = type(obj) | |
if t == "number" then | |
lua = lua .. obj | |
elseif t == "boolean" then | |
lua = lua .. tostring(obj) | |
elseif t == "string" then | |
lua = lua .. string.format("%q", obj) | |
elseif t == "table" then | |
lua = lua .. "{\n" | |
for k, v in pairs(obj) do | |
lua = lua .. "[" .. serialize(k) .. "]=" .. serialize(v) .. ",\n" | |
end | |
local metatable = getmetatable(obj) | |
if metatable ~= nil and type(metatable.__index) == "table" then | |
for k, v in pairs(metatable.__index) do | |
lua = lua .. "[" .. serialize(k) .. "]=" .. serialize(v) .. ",\n" | |
end | |
end | |
lua = lua .. "}" | |
elseif t == "nil" then | |
return nil | |
else | |
error("can not serialize a " .. t .. " type.") | |
end | |
return lua | |
end | |
function unserialize(lua) | |
local t = type(lua) | |
if t == "nil" or lua == "" then | |
return nil | |
elseif t == "number" or t == "string" or t == "boolean" then | |
lua = tostring(lua) | |
else | |
error("can not unserialize a " .. t .. " type.") | |
end | |
lua = "return " .. lua | |
local func = loadstring(lua) | |
if func == nil then | |
return nil | |
end | |
return func() | |
end | |
function split(s, delimiter) | |
result = {}; | |
for match in (s..delimiter):gmatch("(.-)"..delimiter) do | |
table.insert(result, match); | |
end | |
return result; | |
end | |
math.randomseed(tostring(os.time()):reverse():sub(1, 6)) | |
function random_val(_start, _end) | |
return math.random(_start, _end) | |
end | |
function gen_imei() | |
local ret = "" | |
local pos | |
local str = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } | |
local sum = 0 | |
local final_digit = 0 | |
local t = 0 | |
local len_offset = 0 | |
local len = 15 | |
local rbi = { "01", "10", "30", "33", "35", "44", "45", "49", "50", "51", "52", "53", "54", "86", "91", "98", "99" } | |
local arr = rbi[random_val(1, #rbi)] | |
print(arr) | |
str[1] = tonumber(string.sub(arr, 1, 1)) | |
str[2] = tonumber(string.sub(arr, 2)) | |
pos = 3 | |
while pos < len do | |
str[pos] = random_val(0, 9) | |
pos = pos + 1 | |
end | |
len_offset = (len + 1) % 2; | |
pos = 1 | |
while pos < len do | |
print(pos) | |
if (pos + len_offset) % 2 == 0 then | |
t = str[pos] * 2 | |
if t > 9 then | |
t = t - 9 | |
end | |
sum = sum + t | |
else | |
sum = sum + str[pos] | |
end | |
pos = pos + 1 | |
end | |
final_digit = (10 - (sum % 10)) % 10 | |
str[len] = final_digit | |
for _,v in ipairs(str) do | |
ret = ret .. tostring(v) | |
end | |
return ret | |
end | |
print(gen_imei()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment