Skip to content

Commit

Permalink
change: read 8bytes from /dev/urandom as the random seed for better r…
Browse files Browse the repository at this point in the history
…andomness (#2437)
  • Loading branch information
tokers committed Oct 19, 2020
1 parent 943be09 commit 71dcf36
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions apisix/core/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion t/core/utils.t
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down

0 comments on commit 71dcf36

Please sign in to comment.