some generation code optimizations

passed - i_pad and o_pad can be written as just string.char(0x36):rep(64) and string.char(0x5c):rep(64) respectively.
assertion error - The loop for concatenating i_key_pad and message is pointless, you could directly do i_key_pad .. message. Same for the second concat loop.
passed - left_pad could be written as just s:rep(#str - len) .. str.
passed - The loop in generate_secret could be replaced with just buf:sub(1, 20)

Thanks https://forum.minetest.net/viewtopic.php?p=429245#p429245
This commit is contained in:
localhost_frssoft 2023-10-02 14:48:18 +03:00
parent 8bd6172234
commit 2ba6870b68
1 changed files with 4 additions and 12 deletions

View File

@ -16,10 +16,8 @@ end
-- https://en.wikipedia.org/wiki/HMAC
local i_pad = ""
local o_pad = ""
for _=1,64 do
i_pad = i_pad .. string.char(0x36)
o_pad = o_pad .. string.char(0x5c)
end
i_pad = string.char(0x36):rep(64)
o_pad = string.char(0x5c):rep(64)
-- hmac generation
function fediauth.hmac(key, message)
@ -60,10 +58,7 @@ function fediauth.hmac(key, message)
end
local function left_pad(str, s, len)
while #str < len do
str = s .. str
end
return str
return s:rep(#str - len) .. str
end
function fediauth.generate_tfediauth(secret_b32, unix_time)
@ -125,10 +120,7 @@ end
function fediauth.generate_secret()
local buf = minetest.sha1("" .. math.random(10000), true)
local s = ""
for i=1,20 do
s = s .. string.char(string.byte(buf, i))
end
local s = buf:sub(1, 20)
return s
end