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

refactor(tools/rand): speed up random string generation #13150

Closed
wants to merge 3 commits into from
Closed
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
17 changes: 13 additions & 4 deletions kong/tools/rand.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,19 @@ do
-- get 24 bytes, which will return a 32 char string after encoding
-- this is done in attempt to maintain backwards compatibility as
-- much as possible while improving the strength of this function
return encode_base64(get_rand_bytes(24, true))
:gsub("/", char(rand(48, 57))) -- 0 - 10
:gsub("+", char(rand(65, 90))) -- A - Z
:gsub("=", char(rand(97, 122))) -- a - z
local str = encode_base64(get_rand_bytes(24, true))

if str:find("/", 1, true) then
str = str:gsub("/", char(rand(48, 57))) -- 0 - 10
end

if str:find("+", 1, true) then
str = str:gsub("+", char(rand(65, 90))) -- A - Z
end

-- no gsub for "=" since no padding

return str
end

end
Expand Down
Loading