Module: PassGen

Defined in:
lib/dgen/passgen.rb

Overview

Provides the password generation module for use as a mixin.

Class Method Summary collapse

Class Method Details

.batch(n_words, p_length) ⇒ Object

Produces and displays multiple passphrases.



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dgen/passgen.rb', line 82

def self.batch(n_words, p_length)
  f = open_wordlist
  phrase = []
  print 'How many passphrases to generate? => '
  num_pass = gets.chomp.to_i
  num_pass.times do |i|
    phrase.push(make_phrase(n_words, p_length, f))
    puts "Passphrase with spaces:    '#{phrase[i]}'"
    puts "Passphrase without spaces: '#{phrase[i].delete(' ')}'"
  end
  f.close
  phrase
end

.find_word(file, number) ⇒ Object

Chooses words from the diceware word list for the passphrase.



43
44
45
46
47
48
49
50
# File 'lib/dgen/passgen.rb', line 43

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

.make_phrase(n_words, p_length, file) ⇒ Object

Generates and returns the passphrase.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dgen/passgen.rb', line 55

def self.make_phrase(n_words, p_length, file)
  loop do
    words = []
    n_words.times do
      words.push(find_word(file, roll_nums))
    end
    @pass_phrase = words.join(' ')
    break unless @pass_phrase.length < p_length
  end
  @pass_phrase
end

.open_wordlistObject

Opens and returns the file containing the diceware word list.



31
32
33
34
35
36
37
38
# File 'lib/dgen/passgen.rb', line 31

def self.open_wordlist
  path = File.expand_path(File.join(File.dirname(__FILE__),
                                    '..',
                                    'assets',
                                    'word-list.txt'))
  wordlist = File.new(path, 'r')
  wordlist
end

.roll_numsObject

Creates an array of random numbers generated securely.



19
20
21
22
23
24
25
26
# File 'lib/dgen/passgen.rb', line 19

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

.single(n_words, p_length) ⇒ Object

Produces and displays a single passphrase.



70
71
72
73
74
75
76
77
# File 'lib/dgen/passgen.rb', line 70

def self.single(n_words, p_length)
  f = open_wordlist
  phrase = make_phrase(n_words, p_length, f)
  puts "Passphrase with spaces:    '#{phrase}'"
  puts "Passphrase without spaces: '#{phrase.delete(' ')}'"
  f.close
  phrase
end