Class: Passphrase::DicewareRandom
- Inherits:
-
Object
- Object
- Passphrase::DicewareRandom
- Defined in:
- lib/passphrase/diceware_random.rb
Overview
The DicewareRandom class supplies random numbers in two different formats through two instance methods #indices and #die_rolls for use by the DicewareMethod class. Depending on the value of the flag used to instantiate the DicewareRandom class, the unformatted raw random numbers are generated either by the standard SecureRandom class or retrieved from the RANDOM.ORG web site.
Class Attribute Summary collapse
-
.random_org_requests ⇒ Integer
The number of times the RANDOM.ORG site is accessed by all instances of the DicewareRandom class.
Instance Method Summary collapse
-
#die_rolls(number_of_words) ⇒ Array<String>
Returns an array of strings where each string comprises five numeric characters, each one representing one roll of a die.
-
#indices(number_of_words, number_of_languages) ⇒ Array<Integer>
Returns an array of random numbers that can index into the array of available languages.
-
#initialize(use_random_org) ⇒ DicewareRandom
constructor
A new instance of DicewareRandom.
Constructor Details
#initialize(use_random_org) ⇒ DicewareRandom
Returns a new instance of DicewareRandom.
22 23 24 25 |
# File 'lib/passphrase/diceware_random.rb', line 22 def initialize(use_random_org) @random_org_uri = "https://www.random.org" use_random_org ? setup_remote_generator : setup_local_generator end |
Class Attribute Details
.random_org_requests ⇒ Integer
Returns the number of times the RANDOM.ORG site is accessed by all instances of the DicewareRandom class.
15 16 17 |
# File 'lib/passphrase/diceware_random.rb', line 15 def random_org_requests @random_org_requests end |
Instance Method Details
#die_rolls(number_of_words) ⇒ Array<String>
Returns an array of strings where each string comprises five numeric characters, each one representing one roll of a die. The number of elements in the array equals the number of words specified for the passphrase.
46 47 48 49 50 51 52 |
# File 'lib/passphrase/diceware_random.rb', line 46 def die_rolls(number_of_words) # The Diceware method specifies five rolls of the die for each word. die_rolls_per_word = 5 total_die_rolls = number_of_words * die_rolls_per_word die_roll_sequence = generate_random_numbers(total_die_rolls, 6, 1) group_die_rolls(die_roll_sequence, number_of_words, die_rolls_per_word) end |
#indices(number_of_words, number_of_languages) ⇒ Array<Integer>
Returns an array of random numbers that can index into the array of available languages. The number of elements in the array equals the number of words specified for the passphrase.
34 35 36 |
# File 'lib/passphrase/diceware_random.rb', line 34 def indices(number_of_words, number_of_languages) generate_random_numbers(number_of_words, number_of_languages - 1) end |