diff options
Diffstat (limited to 'lib/securerandom.rb')
| -rw-r--r-- | lib/securerandom.rb | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/lib/securerandom.rb b/lib/securerandom.rb index 07ae048634..6079fdb5c4 100644 --- a/lib/securerandom.rb +++ b/lib/securerandom.rb @@ -39,28 +39,34 @@ require 'random/formatter' # +NotImplementedError+ is raised. module SecureRandom + + # The version + VERSION = "0.4.1" + class << self + # Returns a random binary string containing +size+ bytes. + # + # See Random.bytes def bytes(n) return gen_random(n) end + # Compatibility methods for Ruby 3.2, we can remove this after dropping to support Ruby 3.2 + def alphanumeric(n = nil, chars: ALPHANUMERIC) + n = 16 if n.nil? + choose(chars, n) + end if RUBY_VERSION < '3.3' + private + # :stopdoc: + + # Implementation using OpenSSL def gen_random_openssl(n) - @pid = 0 unless defined?(@pid) - pid = $$ - unless @pid == pid - now = Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond) - OpenSSL::Random.random_add([now, @pid, pid].join(""), 0.0) - seed = Random.urandom(16) - if (seed) - OpenSSL::Random.random_add(seed, 16) - end - @pid = pid - end return OpenSSL::Random.random_bytes(n) end + # Implementation using system random device def gen_random_urandom(n) ret = Random.urandom(n) unless ret @@ -86,6 +92,9 @@ module SecureRandom end end + # :startdoc: + + # Generate random data bytes for Random::Formatter public :gen_random end end |
