Module: Dgen::Diceware

Defined in:
lib/dgen/diceware.rb

Overview

Diceware

Author

Dick Davis

Copyright

Copyright 2015-2018 Dick Davis

License

GNU Public License 3

Methods to generate random numbers, select words from word list using those numbers, and combine words into a passphrase.

The algorithm used to generate passwords is the Diceware method, developed by Arnold Reinhold.

Class Method Summary collapse

Class Method Details

.find_word(number, word_list) ⇒ Object

Chooses words from the diceware word list for the passphrase.



47
48
49
50
51
52
53
54
# File 'lib/dgen/diceware.rb', line 47

def self.find_word(number, word_list)
  File.foreach(word_list) do |line|
    num = line.slice(0, 5)
    @word = line.slice(6..-2)
    break if num == number
  end
  @word
end

.make_phrase(n_words, n_chars, word_list) ⇒ Object

Generates and returns the passphrase.



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dgen/diceware.rb', line 58

def self.make_phrase(n_words, n_chars, word_list)
  passphrase = ''
  loop do
    words = []
    n_words.times do
      words.push(find_word(roll_nums, word_list))
    end
    passphrase = words.join(' ')
    break unless passphrase.length < n_chars
  end
  passphrase
end

.roll_numsObject

Creates an array of random numbers generated securely.



36
37
38
39
40
41
42
43
# File 'lib/dgen/diceware.rb', line 36

def self.roll_nums
  numbers = []
  5.times do
    numbers.push(SecureRandom.random_number(6) + 1)
  end
  num = numbers.join('')
  num
end