123456789_123456789_123456789_123456789_123456789_

Module: Gem::SecureRandom

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Random::Formatter
Defined in: lib/rubygems/vendor/securerandom/lib/securerandom.rb

Constant Summary

Class Method Summary

Class Method Details

.alphanumeric(n = nil, chars: ALPHANUMERIC)

Compatibility methods for Ruby 3.2, we can remove this after dropping to support Ruby 3.2

[ GitHub ]

  
# File 'lib/rubygems/vendor/securerandom/lib/securerandom.rb', line 59

def alphanumeric(n = nil, chars: ALPHANUMERIC)
  n = 16 if n.nil?
  choose(chars, n)
end

.bytes(n)

Returns a random binary string containing size bytes.

See Random.bytes

[ GitHub ]

  
# File 'lib/rubygems/vendor/securerandom/lib/securerandom.rb', line 54

def bytes(n)
  return gen_random(n)
end

.gen_random_openssl(n) (private)

This method is for internal use only.

Implementation using OpenSSL

[ GitHub ]

  
# File 'lib/rubygems/vendor/securerandom/lib/securerandom.rb', line 69

def gen_random_openssl(n)
  return OpenSSL::Random.random_bytes(n)
end

.gen_random_urandom(n) (private)

This method is for internal use only.

Implementation using system random device

[ GitHub ]

  
# File 'lib/rubygems/vendor/securerandom/lib/securerandom.rb', line 74

def gen_random_urandom(n)
  ret = Random.urandom(n)
  unless ret
    raise NotImplementedError, "No random device"
  end
  unless ret.length == n
    raise NotImplementedError, "Unexpected partial read from random device: only #{ret.length} for #{n} bytes"
  end
  ret
end