Class: Passphrase::DicewareRandom

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(use_random_org) ⇒ DicewareRandom

Returns a new instance of DicewareRandom.

Parameters:

  • use_random_org (Boolean)

    a flag that triggers the use of RANDOM.ORG for generating random numbers



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_requestsInteger

Returns the number of times the RANDOM.ORG site is accessed by all instances of the DicewareRandom class.

Returns:

  • (Integer)

    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.

Parameters:

  • number_of_words (Integer)

    the desired number of words in the passphrase

Returns:

  • (Array<String>)

    an array of strings each one of which represents five rolls of a die



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.

Parameters:

  • number_of_words (Integer)

    the desired number of words in the passphrase

  • number_of_languages (Integer)

    the number of available languages

Returns:

  • (Array<Integer>)

    an array of random number indices



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