From 71dcf367d39225c2bca9a2221611e0a8cfa35eca Mon Sep 17 00:00:00 2001 From: Alex Zhang Date: Mon, 19 Oct 2020 14:53:48 +0800 Subject: [PATCH] change: read 8bytes from /dev/urandom as the random seed for better randomness (#2437) --- apisix/core/utils.lua | 5 +++-- t/core/utils.t | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/apisix/core/utils.lua b/apisix/core/utils.lua index 61963a9e0ad7..a7cad379e629 100644 --- a/apisix/core/utils.lua +++ b/apisix/core/utils.lua @@ -52,16 +52,17 @@ function _M.get_seed_from_urandom() return nil, 'failed to open /dev/urandom: ' .. err end - local str = frandom:read(4) + local str = frandom:read(8) frandom:close() if not str then return nil, 'failed to read data from /dev/urandom' end local seed = 0 - for i = 1, 4 do + for i = 1, 8 do seed = 256 * seed + str:byte(i) end + return seed end diff --git a/t/core/utils.t b/t/core/utils.t index 25b0419b258b..817db25af277 100644 --- a/t/core/utils.t +++ b/t/core/utils.t @@ -38,7 +38,7 @@ __DATA__ --- request GET /t --- response_body_like eval -qr/random seed \d+\ntwice: false/ +qr/random seed \d+(\.\d+)?(e\+\d+)?\ntwice: false/