123456789_123456789_123456789_123456789_123456789_

Module: Bundler::SecureRandom

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Defined in: lib/bundler/vendor/securerandom/lib/securerandom.rb

Overview

Secure random number generator interface.

This library is an interface to secure random number generators which are suitable for generating session keys in HTTP cookies, etc.

You can use this library in your application by requiring it:

require 'bundler/vendor/securerandom/lib/securerandom'

It supports the following secure random number generators:

  • openssl

  • /dev/urandom

  • Win32

SecureRandom is extended by the Random::Formatter module which defines the following methods:

  • alphanumeric

  • base64

  • choose

  • gen_random

  • hex

  • rand

  • random_bytes

  • random_number

  • urlsafe_base64

  • uuid

These methods are usable as class methods of SecureRandom such as Bundler::SecureRandom.hex.

If a secure random number generator is not available, NotImplementedError is raised.

Constant Summary

Class Method Summary

Random::Formatter - Extended

alphanumeric

Generate a random alphanumeric string.

base64

Generate a random base64 string.

hex

Generate a random hexadecimal string.

random_bytes

Generate a random binary string.

urlsafe_base64

Generate a random URL-safe base64 string.

uuid

Generate a random v4 UUID (Universally Unique IDentifier).

uuid_v4
uuid_v7

Generate a random v7 UUID (Universally Unique IDentifier).

choose

Generate a string that randomly draws from a source array of characters.

gen_random

Internal interface to Random; Generate random data n bytes.

Class Method Details

.bytes(n)

Returns a random binary string containing size bytes.

See Random.bytes

[ GitHub ]

  
# File 'lib/bundler/vendor/securerandom/lib/securerandom.rb', line 50

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/bundler/vendor/securerandom/lib/securerandom.rb', line 59

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/bundler/vendor/securerandom/lib/securerandom.rb', line 64

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