From ff8aea69cb351624c895f3de8ff1ca4edcb63579 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Tue, 4 Jun 2024 08:20:21 +0800 Subject: [PATCH 1/3] find before gsub --- kong/tools/rand.lua | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/kong/tools/rand.lua b/kong/tools/rand.lua index cfb4bfbf3409..d418dcc2bbdc 100644 --- a/kong/tools/rand.lua +++ b/kong/tools/rand.lua @@ -120,10 +120,25 @@ 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 = 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 + + if str:find("=", 1, true) then + str = str:gsub("=", char(rand(97, 122))) -- a - z + end + + return str + --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 end From 4805bde8aea81541ae7fe52e8efabd23bb056959 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Tue, 4 Jun 2024 08:27:18 +0800 Subject: [PATCH 2/3] clean --- kong/tools/rand.lua | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/kong/tools/rand.lua b/kong/tools/rand.lua index d418dcc2bbdc..c6d852b963f6 100644 --- a/kong/tools/rand.lua +++ b/kong/tools/rand.lua @@ -120,7 +120,7 @@ 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 - local str = get_rand_bytes(24, true) + local str = encode_base64(get_rand_bytes(24, true)) if str:find("/", 1, true) then str = str:gsub("/", char(rand(48, 57))) -- 0 - 10 @@ -135,10 +135,6 @@ do end return str - --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 end From 3e0a707a897631becd0256a78fbf9068746c3702 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 6 Jun 2024 08:59:59 +0800 Subject: [PATCH 3/3] remove gsub for "=" --- kong/tools/rand.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/kong/tools/rand.lua b/kong/tools/rand.lua index c6d852b963f6..c32b8b5a96f2 100644 --- a/kong/tools/rand.lua +++ b/kong/tools/rand.lua @@ -130,9 +130,7 @@ do str = str:gsub("+", char(rand(65, 90))) -- A - Z end - if str:find("=", 1, true) then - str = str:gsub("=", char(rand(97, 122))) -- a - z - end + -- no gsub for "=" since no padding return str end