Class: PasswordListGenerator::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/password_list_generator/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Generator

Returns a new instance of Generator.



5
6
7
8
# File 'lib/password_list_generator/generator.rb', line 5

def initialize(config)
  @config = config
  build_characters_set
end

Instance Attribute Details

#characters_setObject

Returns the value of attribute characters_set.



3
4
5
# File 'lib/password_list_generator/generator.rb', line 3

def characters_set
  @characters_set
end

#configObject

Returns the value of attribute config.



3
4
5
# File 'lib/password_list_generator/generator.rb', line 3

def config
  @config
end

Instance Method Details

#generateObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/password_list_generator/generator.rb', line 10

def generate
  passwords = []

  1.upto(config.count) do
    valid  = false
    random = ''

    until valid
      random_size = (config.min..config.max).to_a.shuffle.first
      random      = (1..random_size).map {|a| characters_set[SecureRandom.random_number(characters_set.size)]}.join
      password    = Password.new(random, config)

      if password.valid?
        passwords << password
        valid = true 
      end
    end
  end

  passwords
end