Skip to content

Commit 5cf8d7e

Browse files
Update EncryptStrings.lua
1 parent 3166872 commit 5cf8d7e

1 file changed

Lines changed: 102 additions & 69 deletions

File tree

src/prometheus/steps/EncryptStrings.lua

Lines changed: 102 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ function EncryptStrings:init(_) end
2424
function EncryptStrings:CreateEncryptionService()
2525
local usedSeeds = {};
2626

27-
local secret_key_6 = math.random(0, 63) -- 6-bit arbitrary integer (0..63)
28-
local secret_key_7 = math.random(0, 127) -- 7-bit arbitrary integer (0..127)
29-
local secret_key_44 = math.random(0, 17592186044415) -- 44-bit arbitrary integer (0..17592186044415)
30-
local secret_key_8 = math.random(0, 255); -- 8-bit arbitrary integer (0..255)
27+
local secret_key_6 = math.random(0, 63)
28+
local secret_key_7 = math.random(0, 127)
29+
local secret_key_44 = math.random(0, 17592186044415)
30+
local secret_key_8 = math.random(0, 255);
3131

3232
local floor = math.floor
33+
local byte = string.byte
34+
local char = string.char
35+
local concat = table.concat
3336

3437
local function primitive_root_257(idx)
3538
local g, m, d = 1, 128, 2 * idx + 1
@@ -47,62 +50,73 @@ function EncryptStrings:CreateEncryptionService()
4750
local state_8 = 2
4851

4952
local prev_values = {}
53+
local prev_index = 0
54+
5055
local function set_seed(seed_53)
5156
state_45 = seed_53 % 35184372088832
5257
state_8 = seed_53 % 255 + 2
5358
prev_values = {}
59+
prev_index = 0
5460
end
5561

5662
local function gen_seed()
57-
local seed;
63+
local seed
5864
repeat
59-
seed = math.random(0, 35184372088832);
60-
until not usedSeeds[seed];
61-
usedSeeds[seed] = true;
62-
return seed;
65+
seed = math.random(0, 35184372088832)
66+
until not usedSeeds[seed]
67+
usedSeeds[seed] = true
68+
return seed
6369
end
6470

6571
local function get_random_32()
6672
state_45 = (state_45 * param_mul_45 + param_add_45) % 35184372088832
6773
repeat
6874
state_8 = state_8 * param_mul_8 % 257
6975
until state_8 ~= 1
76+
7077
local r = state_8 % 32
7178
local n = floor(state_45 / 2 ^ (13 - (state_8 - r) / 32)) % 2 ^ 32 / 2 ^ r
7279
return floor(n % 1 * 2 ^ 32) + floor(n)
7380
end
7481

7582
local function get_next_pseudo_random_byte()
76-
if #prev_values == 0 then
77-
local rnd = get_random_32() -- value 0..4294967295
83+
if prev_index == 0 then
84+
local rnd = get_random_32()
7885
local low_16 = rnd % 65536
7986
local high_16 = (rnd - low_16) / 65536
8087
local b1 = low_16 % 256
8188
local b2 = (low_16 - b1) / 256
8289
local b3 = high_16 % 256
8390
local b4 = (high_16 - b3) / 256
8491
prev_values = { b1, b2, b3, b4 }
92+
prev_index = 4
8593
end
86-
--print(unpack(prev_values))
87-
return table.remove(prev_values)
94+
95+
local v = prev_values[prev_index]
96+
prev_index = prev_index - 1
97+
return v
8898
end
8999

90100
local function encrypt(str)
91-
local seed = gen_seed();
101+
local seed = gen_seed()
92102
set_seed(seed)
93-
local len = string.len(str)
103+
104+
local len = #str
94105
local out = {}
95-
local prevVal = secret_key_8;
106+
local prevVal = secret_key_8
107+
96108
for i = 1, len do
97-
local byte = string.byte(str, i);
98-
out[i] = string.char((byte - (get_next_pseudo_random_byte() + prevVal)) % 256);
99-
prevVal = byte;
109+
local b = byte(str, i)
110+
local r = get_next_pseudo_random_byte()
111+
out[i] = char((b - (r + prevVal)) % 256)
112+
prevVal = b
100113
end
101-
return table.concat(out), seed;
114+
115+
return concat(out), seed
102116
end
103117

104-
local function genCode()
105-
local code = [[
118+
local function genCode()
119+
local code = [[
106120
do
107121
]] .. table.concat(util.shuffle{
108122
"local floor = math.floor",
@@ -125,37 +139,47 @@ do
125139
until #nums == 0;
126140
127141
local prev_values = {}
142+
local prev_index = 0
143+
128144
local function get_next_pseudo_random_byte()
129-
if #prev_values == 0 then
145+
if prev_index == 0 then
130146
state_45 = (state_45 * ]] .. tostring(param_mul_45) .. [[ + ]] .. tostring(param_add_45) .. [[) % 35184372088832
131147
repeat
132148
state_8 = state_8 * ]] .. tostring(param_mul_8) .. [[ % 257
133149
until state_8 ~= 1
150+
134151
local r = state_8 % 32
135152
local shift = 13 - (state_8 - r) / 32
136153
local n = floor(state_45 / 2 ^ shift) % 4294967296 / 2 ^ r
137154
local rnd = floor(n % 1 * 4294967296) + floor(n)
155+
138156
local low_16 = rnd % 65536
139157
local high_16 = (rnd - low_16) / 65536
140-
prev_values = { low_16 % 256, (low_16 - low_16 % 256) / 256, high_16 % 256, (high_16 - high_16 % 256) / 256 }
158+
prev_values = {
159+
low_16 % 256,
160+
(low_16 - low_16 % 256) / 256,
161+
high_16 % 256,
162+
(high_16 - high_16 % 256) / 256
163+
}
164+
prev_index = 4
141165
end
142166
143-
144-
local prevValuesLen = #prev_values;
145-
local removed = prev_values[prevValuesLen];
146-
prev_values[prevValuesLen] = nil;
147-
return removed;
167+
local v = prev_values[prev_index]
168+
prev_index = prev_index - 1
169+
return v
148170
end
149171
150172
local realStrings = {};
151173
STRINGS = setmetatable({}, {
152174
__index = realStrings;
153175
__metatable = nil;
154176
});
155-
function DECRYPT(str, seed)
177+
178+
function DECRYPT(str, seed)
156179
local realStringsLocal = realStrings;
157180
if(realStringsLocal[seed]) then return seed; else
158181
prev_values = {};
182+
prev_index = 0;
159183
local chars = charmap;
160184
state_45 = seed % 35184372088832
161185
state_8 = seed % 255 + 2
@@ -173,66 +197,75 @@ do
173197
end
174198
end]]
175199

176-
return code;
177-
end
200+
return code
201+
end
178202

179-
return {
180-
encrypt = encrypt,
181-
param_mul_45 = param_mul_45,
182-
param_mul_8 = param_mul_8,
183-
param_add_45 = param_add_45,
203+
return {
204+
encrypt = encrypt,
205+
param_mul_45 = param_mul_45,
206+
param_mul_8 = param_mul_8,
207+
param_add_45 = param_add_45,
184208
secret_key_8 = secret_key_8,
185-
genCode = genCode,
186-
}
209+
genCode = genCode,
210+
}
187211
end
188212

189213
function EncryptStrings:apply(ast, _)
190-
local Encryptor = self:CreateEncryptionService();
214+
local Encryptor = self:CreateEncryptionService()
191215

192-
local code = Encryptor.genCode();
193-
local newAst = Parser:new({ LuaVersion = Enums.LuaVersion.Lua51 }):parse(code);
194-
local doStat = newAst.body.statements[1];
216+
local code = Encryptor.genCode()
217+
local newAst = Parser:new({ LuaVersion = Enums.LuaVersion.Lua51 }):parse(code)
218+
local doStat = newAst.body.statements[1]
195219

196-
local scope = ast.body.scope;
197-
local decryptVar = scope:addVariable();
198-
local stringsVar = scope:addVariable();
220+
local scope = ast.body.scope
221+
local decryptVar = scope:addVariable()
222+
local stringsVar = scope:addVariable()
199223

200-
doStat.body.scope:setParent(ast.body.scope);
224+
doStat.body.scope:setParent(ast.body.scope)
201225

202226
visitast(newAst, nil, function(node, data)
203-
if(node.kind == AstKind.FunctionDeclaration) then
204-
if(node.scope:getVariableName(node.id) == "DECRYPT") then
205-
data.scope:removeReferenceToHigherScope(node.scope, node.id);
206-
data.scope:addReferenceToHigherScope(scope, decryptVar);
207-
node.scope = scope;
208-
node.id = decryptVar;
227+
if node.kind == AstKind.FunctionDeclaration then
228+
if node.scope:getVariableName(node.id) == "DECRYPT" then
229+
data.scope:removeReferenceToHigherScope(node.scope, node.id)
230+
data.scope:addReferenceToHigherScope(scope, decryptVar)
231+
node.scope = scope
232+
node.id = decryptVar
209233
end
210234
end
211-
if(node.kind == AstKind.AssignmentVariable or node.kind == AstKind.VariableExpression) then
212-
if(node.scope:getVariableName(node.id) == "STRINGS") then
213-
data.scope:removeReferenceToHigherScope(node.scope, node.id);
214-
data.scope:addReferenceToHigherScope(scope, stringsVar);
215-
node.scope = scope;
216-
node.id = stringsVar;
235+
236+
if node.kind == AstKind.AssignmentVariable or node.kind == AstKind.VariableExpression then
237+
if node.scope:getVariableName(node.id) == "STRINGS" then
238+
data.scope:removeReferenceToHigherScope(node.scope, node.id)
239+
data.scope:addReferenceToHigherScope(scope, stringsVar)
240+
node.scope = scope
241+
node.id = stringsVar
217242
end
218243
end
219244
end)
220245

221246
visitast(ast, nil, function(node, data)
222-
if(node.kind == AstKind.StringExpression) then
223-
data.scope:addReferenceToHigherScope(scope, stringsVar);
224-
data.scope:addReferenceToHigherScope(scope, decryptVar);
225-
local encrypted, seed = Encryptor.encrypt(node.value);
226-
return Ast.IndexExpression(Ast.VariableExpression(scope, stringsVar), Ast.FunctionCallExpression(Ast.VariableExpression(scope, decryptVar), {
227-
Ast.StringExpression(encrypted), Ast.NumberExpression(seed),
228-
}));
247+
if node.kind == AstKind.StringExpression then
248+
data.scope:addReferenceToHigherScope(scope, stringsVar)
249+
data.scope:addReferenceToHigherScope(scope, decryptVar)
250+
251+
local encrypted, seed = Encryptor.encrypt(node.value)
252+
253+
return Ast.IndexExpression(
254+
Ast.VariableExpression(scope, stringsVar),
255+
Ast.FunctionCallExpression(
256+
Ast.VariableExpression(scope, decryptVar),
257+
{
258+
Ast.StringExpression(encrypted),
259+
Ast.NumberExpression(seed),
260+
}
261+
)
262+
)
229263
end
230264
end)
231265

266+
table.insert(ast.body.statements, 1, doStat)
267+
table.insert(ast.body.statements, 1, Ast.LocalVariableDeclaration(scope, util.shuffle{ decryptVar, stringsVar }, {}))
232268

233-
-- Insert to Main Ast
234-
table.insert(ast.body.statements, 1, doStat);
235-
table.insert(ast.body.statements, 1, Ast.LocalVariableDeclaration(scope, util.shuffle{ decryptVar, stringsVar }, {}));
236269
return ast
237270
end
238271

0 commit comments

Comments
 (0)