Skip to content

Commit 5585bfe

Browse files
committed
little optimization on hex2bin and bin2hex
1 parent 6adac17 commit 5585bfe

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

sha2.lua

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4706,20 +4706,18 @@ end
47064706

47074707
local hex_to_bin, bin_to_hex, bin_to_base64, base64_to_bin
47084708
do
4709+
local function repl_hex2bin(hh)
4710+
return char(tonumber(hh, 16))
4711+
end
47094712
function hex_to_bin(hex_string)
4710-
return (gsub(hex_string, "%x%x",
4711-
function (hh)
4712-
return char(tonumber(hh, 16))
4713-
end
4714-
))
4713+
return (gsub(hex_string, "%x%x", repl_hex2bin))
47154714
end
47164715

4716+
local function repl_bin2hex(c)
4717+
return string_format("%02x", byte(c))
4718+
end
47174719
function bin_to_hex(binary_string)
4718-
return (gsub(binary_string, ".",
4719-
function (c)
4720-
return string_format("%02x", byte(c))
4721-
end
4722-
))
4720+
return (gsub(binary_string, ".", repl_bin2hex))
47234721
end
47244722

47254723
local base64_symbols = {

0 commit comments

Comments
 (0)