module Dgen::Diceware

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.

Public Class Methods

find_word(number, word_list) click to toggle source

Chooses words from the diceware word list for the passphrase.

# 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) click to toggle source

Generates and returns the passphrase.

# 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_nums() click to toggle source

Creates an array of random numbers generated securely.

# 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