From 2ba6870b6807ae27255a9b8d7d6236bbc102af5c Mon Sep 17 00:00:00 2001 From: localhost_frssoft Date: Mon, 2 Oct 2023 14:48:18 +0300 Subject: [PATCH] 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 --- functions.lua | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/functions.lua b/functions.lua index 7572dc5..576caed 100644 --- a/functions.lua +++ b/functions.lua @@ -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