Module: RandomOrg

Defined in:
lib/random_org.rb,
lib/random_org/rng.rb,
lib/random_org/api_error.rb,
lib/random_org/api_client.rb,
lib/random_org/configuration.rb,
lib/random_org/argument_error.rb,
lib/random_org/api_server_error.rb,
lib/random_org/wrong_api_key_error.rb

Overview

This library is an interface to the random.org random number generator API which generates true random numbers through data gathered from atmospheric noise.

This library is implemented as a drop-in replacement for SecureRandom, giving you the same methods with the same parameters and mimicing the behaviour of the corresponding method in SecureRandom.

Defined Under Namespace

Classes: ApiClient, ApiError, ApiServerError, ArgumentError, Configuration, Rng, WrongApiKeyError

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



126
127
128
# File 'lib/random_org.rb', line 126

def configuration
  @configuration
end

Class Method Details

.base64(length = 16) ⇒ Object

RandomOrg.base64 generates a random base64 string.

The length of the result string is about 4/3 of length.

Parameters:

  • length (Numeric) (defaults to: 16)

    the length of the random string, if not specified, 16 is assumed.



85
86
87
88
89
90
91
92
93
# File 'lib/random_org.rb', line 85

def self.base64(length = 16)
  size = length * 8
  req = RandomOrg::ApiClient.build_request(:generate_blobs,
                                           n: 1,
                                           size: size,
                                           format: 'base64')
  response = RandomOrg::ApiClient.perform_request(req)
  process_response(response)
end

.configure {|config| ... } ⇒ Object

Modify the current configuration.

Examples:

RandomOrg.configure do |config|
  config.api_key = "YOUR_API_KEY"
end

Yield Parameters:



27
28
29
30
# File 'lib/random_org.rb', line 27

def self.configure
  self.configuration ||= RandomOrg::Configuration.new
  yield self.configuration
end

.hex(length = 16) ⇒ Object

RandomOrg.hex generates a random hex string.

The length of the result string is twice of length.

Parameters:

  • length (Numeric) (defaults to: 16)

    the length of the random string, if not specified, 16 is assumed.



69
70
71
72
73
74
75
76
77
# File 'lib/random_org.rb', line 69

def self.hex(length = 16)
  size = length * 8
  req = RandomOrg::ApiClient.build_request(:generate_blobs,
                                           n: 1,
                                           size: size,
                                           format: 'hex')
  response = RandomOrg::ApiClient.perform_request(req)
  process_response(response)
end

.random_bytes(length = 16) ⇒ Object

RandomOrg.random_bytes generates a random binary string.

Parameters:

  • length (Numeric) (defaults to: 16)

    the length of the result string, if not specified, 16 is assumed.



36
37
38
# File 'lib/random_org.rb', line 36

def self.random_bytes(length = 16)
  [hex(length)].pack('H*')
end

.random_number(maximum = 0) ⇒ Object

RandomOrg.random_number generates a random number.

If a positive integer is given as maximum, RandomOrg.random_number returns an integer:

+0 <= RandomOrg.random_number(maximum) < maximum+.

If 0 is given or an argument is not given, RandomOrg.random_number returns a float:

+0.0 <= RandomOrg.random_number() < 1.0+.

Parameters:

  • maximum (Numeric) (defaults to: 0)

    maximum if the value given is > 0



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/random_org.rb', line 51

def self.random_number(maximum = 0)
  min = 0
  req = if maximum.zero?
          request_default
        else
          # random.org treats the range as inclusive so set max=max-1
          request_with_min_max(min, maximum - 1)
        end
  response = RandomOrg::ApiClient.perform_request(req)
  process_response(response)
end

.urlsafe_base64(length = nil, padding = false) ⇒ Object

RandomOrg.urlsafe_base64 generates a random URL-safe base64 string.

The length of the result string is about 4/3 of n.

By default, padding is not generated because “=” may be used as a URL delimiter.

Parameters:

  • length (Numeric) (defaults to: nil)

    the length of the random length, if not specified, 16 is assumed.

  • padding (Boolean) (defaults to: false)

    specifies the padding: if false or nil, padding is not generated, otherwise padding is generated.



106
107
108
109
110
111
# File 'lib/random_org.rb', line 106

def self.urlsafe_base64(length = nil, padding = false)
  s = base64 length
  s.tr!('+/', '-_')
  s.delete!('=') unless padding
  s
end

.uuidObject

RandomOrg.uuid generates a v4 random UUID (Universally Unique IDentifier).

The version 4 UUID is purely random (except the version). It doesn’t

contain meaningful information such as MAC address, time, etc.

See RFC 4122 for details of UUID.



119
120
121
122
123
# File 'lib/random_org.rb', line 119

def self.uuid
  req = RandomOrg::ApiClient.build_request(:generate_uuids, n: 1)
  response = RandomOrg::ApiClient.perform_request(req)
  response['result']['random']['data'].first
end