Skip to content

Commit

Permalink
lib/securerandom.rb: Fix the check of availability of Random.urandom
Browse files Browse the repository at this point in the history
Random.urandom raises a RuntimeError if it is unavailable.
[Bug #13885]
  • Loading branch information
mame authored and khasinski committed Nov 3, 2022
1 parent 90e37ec commit 84eeab6
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/securerandom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,18 @@ def gen_random_urandom(n)
ret
end

ret = Random.urandom(1)
if ret.nil?
begin
# Check if Random.urandom is available
Random.urandom(1)
alias gen_random gen_random_urandom
rescue RuntimeError
begin
require 'openssl'
rescue NoMethodError
raise NotImplementedError, "No random device"
else
alias gen_random gen_random_openssl
end
else
alias gen_random gen_random_urandom
end

public :gen_random
Expand Down

0 comments on commit 84eeab6

Please sign in to comment.