mirror of
https://git.phreedom.club/localhost_frssoft/fediauth.git
synced 2025-01-09 01:21:11 +00:00
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:
parent
8bd6172234
commit
2ba6870b68
|
@ -16,10 +16,8 @@ end
|
||||||
-- https://en.wikipedia.org/wiki/HMAC
|
-- https://en.wikipedia.org/wiki/HMAC
|
||||||
local i_pad = ""
|
local i_pad = ""
|
||||||
local o_pad = ""
|
local o_pad = ""
|
||||||
for _=1,64 do
|
i_pad = string.char(0x36):rep(64)
|
||||||
i_pad = i_pad .. string.char(0x36)
|
o_pad = string.char(0x5c):rep(64)
|
||||||
o_pad = o_pad .. string.char(0x5c)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- hmac generation
|
-- hmac generation
|
||||||
function fediauth.hmac(key, message)
|
function fediauth.hmac(key, message)
|
||||||
|
@ -60,10 +58,7 @@ function fediauth.hmac(key, message)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function left_pad(str, s, len)
|
local function left_pad(str, s, len)
|
||||||
while #str < len do
|
return s:rep(#str - len) .. str
|
||||||
str = s .. str
|
|
||||||
end
|
|
||||||
return str
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function fediauth.generate_tfediauth(secret_b32, unix_time)
|
function fediauth.generate_tfediauth(secret_b32, unix_time)
|
||||||
|
@ -125,10 +120,7 @@ end
|
||||||
|
|
||||||
function fediauth.generate_secret()
|
function fediauth.generate_secret()
|
||||||
local buf = minetest.sha1("" .. math.random(10000), true)
|
local buf = minetest.sha1("" .. math.random(10000), true)
|
||||||
local s = ""
|
local s = buf:sub(1, 20)
|
||||||
for i=1,20 do
|
|
||||||
s = s .. string.char(string.byte(buf, i))
|
|
||||||
end
|
|
||||||
return s
|
return s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue