repl.it
@anonymous/

LongtermVioletTakin

Lua

No description

fork
loading
Files
  • main.lua
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
local function reduceQnt(r, ...)
  for i = 1, arg.n do 
    arg[i] = arg[i] - r
  end
  arg.n = nil
  return arg
end

-- Serialize
local function serializeString(data)
	if type(data) ~= 'string' or #data == 0 then return false end
	local summ = table.concat(reduceQnt(21, data:byte(1, #data)))
	local serial = ""
	local maxValue = (2^31) -1 -- int32
	
	repeat
		local lines = 10
		local tmp = summ:sub(-lines)	  
		while (tonumber(tmp) > maxValue or tmp:sub(1, 1) == "0") do
			lines = lines - 1
			tmp = summ:sub(-lines)
		end	  
		serial = tmp..serial
		summ = summ:sub(1, -(lines+1))
	until summ == ""
	
	return serial
end

-- Unserialize
local function unserializeString(serial)
	local index = 1
	local data = ""
	
	while (index < serial:len()) do
		local char = serial:sub(index, index + 1)
		if tonumber(char) < 11 then
			char = serial:sub(index, index + 2)
			index = index + 1
		end
		index = index + 2
		data = data..string.char(char + 21)
	end
	
	return data
end


-- Como usar:
local entrada = "Todos saudam o esquilo"
local sr = serializeString(entrada)
print(sr)
print(unserializeString(sr))
Fetching token
?