Class: PwFoo::GeneratePassword

Inherits:
Object
  • Object
show all
Defined in:
lib/pwfoo/generate_password.rb

Instance Method Summary collapse

Constructor Details

#initialize(length = 10, *allowed_types) ⇒ GeneratePassword

Returns a new instance of GeneratePassword.



14
15
16
17
18
19
# File 'lib/pwfoo/generate_password.rb', line 14

def initialize(length = 10, *allowed_types)
  @length = length
  @allowed_types = allowed_types.nil? || 0 == allowed_types.length ? [LOWER_CASE, UPPER_CASE, NUMBERS] : allowed_types
  @pw_strength_finder = PasswordStrength.new
  srand(SrandSeedGenerator.new().get_next_seed)
end

Instance Method Details

#generateObject



29
30
31
32
33
34
35
# File 'lib/pwfoo/generate_password.rb', line 29

def generate
  gen_pw = ''
  @length.times { |i|
    gen_pw.concat next_char
  }
  gen_pw
end

#generate_with_min_strength(min_strength) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/pwfoo/generate_password.rb', line 21

def generate_with_min_strength(min_strength)
  gen = nil
  while gen == nil || min_strength > @pw_strength_finder.calculate_score(gen) do
    gen = generate
  end
  gen
end