Class: Passphrase::PassphraseString

Inherits:
String
  • Object
show all
Defined in:
lib/passphrase/passphrase_string.rb

Overview

This subclass of String implements the #to_password instance method for substituting a capital letter, number, and special character into a passphrase.

Instance Method Summary collapse

Constructor Details

#initialize(passphrase, use_random_org) ⇒ PassphraseString

Returns a new instance of PassphraseString.

Parameters:

  • passphrase (String)

    the passphrase as an ordinary String object

  • use_random_org (Boolean)

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



9
10
11
12
13
# File 'lib/passphrase/passphrase_string.rb', line 9

def initialize(passphrase, use_random_org)
  super(passphrase)
  @use_random_org = use_random_org
  @random = DicewareRandom.new(use_random_org)
end

Instance Method Details

#to_passwordString

Returns a new String that is the passwordized version of the passphrase.

Returns:

  • (String)

    a new String that is the passwordized version of the passphrase



17
18
19
20
21
22
23
24
# File 'lib/passphrase/passphrase_string.rb', line 17

def to_password
  @scratch = self.gsub(/\s/, "_")
  capital_index, number_index, special_index = @random.indices(3, @scratch.length)
  select_and_substitute_capital_letter(capital_index)
  select_and_substitute_number(number_index)
  select_and_substitute_special_character(special_index)
  String.new(@scratch)
end