Skip to content

Commit

Permalink
refactor(tools/rand): speed up random string generation, take 2 (#13186)
Browse files Browse the repository at this point in the history
### Summary

This PR optimizes the `kong.tools.rand.random_string`.

Results can be seen in PR that this PR replaces:
#13150 (comment)

KAG-4673

Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
  • Loading branch information
bungle authored Jun 12, 2024
1 parent 582d5ac commit 9a215eb
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions kong/tools/rand.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,44 @@ _M.get_rand_bytes = get_rand_bytes
-- @return string The random string (a chunk of base64ish-encoded random bytes)
local random_string
do
local char = string.char
local rand = math.random
local byte = string.byte
local encode_base64 = ngx.encode_base64

local OUTPUT = require("string.buffer").new(32)
local SLASH_BYTE = byte("/")
local PLUS_BYTE = byte("+")

-- generate a random-looking string by retrieving a chunk of bytes and
-- replacing non-alphanumeric characters with random alphanumeric replacements
-- (we dont care about deriving these bytes securely)
-- this serves to attempt to maintain some backward compatibility with the
-- previous implementation (stripping a UUID of its hyphens), while significantly
-- expanding the size of the keyspace.
random_string = function()
OUTPUT:reset()

-- 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
end
-- TODO: in future we may want to have variable length option to this
local str = encode_base64(get_rand_bytes(24, true), true)

for i = 1, 32 do
local b = byte(str, i)
if b == SLASH_BYTE then
OUTPUT:putf("%c", rand(65, 90)) -- A-Z
elseif b == PLUS_BYTE then
OUTPUT:putf("%c", rand(97, 122)) -- a-z
else
OUTPUT:putf("%c", b)
end
end

str = OUTPUT:get()

return str
end
end
_M.random_string = random_string

Expand Down

1 comment on commit 9a215eb

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:9a215eb6563be78d7ee396d2ec53b4f0b1cc9d66
Artifacts available https://github.com/Kong/kong/actions/runs/9479633967

Please sign in to comment.