Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

little optimization on hex2bin and bin2hex #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions sha2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4706,20 +4706,18 @@ end

local hex_to_bin, bin_to_hex, bin_to_base64, base64_to_bin
do
local function repl_hex2bin(hh)
return char(tonumber(hh, 16))
end
function hex_to_bin(hex_string)
return (gsub(hex_string, "%x%x",
function (hh)
return char(tonumber(hh, 16))
end
))
return (gsub(hex_string, "%x%x", repl_hex2bin))
end

local function repl_bin2hex(c)
return string_format("%02x", byte(c))
end
function bin_to_hex(binary_string)
return (gsub(binary_string, ".",
function (c)
return string_format("%02x", byte(c))
end
))
return (gsub(binary_string, ".", repl_bin2hex))
end

local base64_symbols = {
Expand Down