Class: StrongPassword::StrengthChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/strong_password/strength_checker.rb

Constant Summary collapse

BASE_ENTROPY =
18

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(password) ⇒ StrengthChecker

Returns a new instance of StrengthChecker.



7
8
9
# File 'lib/strong_password/strength_checker.rb', line 7

def initialize(password)
  @base_password = password.dup
end

Instance Attribute Details

#base_passwordObject (readonly)

Returns the value of attribute base_password.



5
6
7
# File 'lib/strong_password/strength_checker.rb', line 5

def base_password
  @base_password
end

Instance Method Details

#calculate_entropy(use_dictionary: false, min_word_length: 4, extra_dictionary_words: []) ⇒ Object



31
32
33
34
35
# File 'lib/strong_password/strength_checker.rb', line 31

def calculate_entropy(use_dictionary: false, min_word_length: 4, extra_dictionary_words: [])
  entropies = [EntropyCalculator.calculate(base_password), EntropyCalculator.calculate(base_password.downcase), QwertyAdjuster.new(base_password).adjusted_entropy]
  entropies << DictionaryAdjuster.new(base_password).adjusted_entropy(min_word_length: min_word_length, extra_dictionary_words: extra_dictionary_words) if use_dictionary
  entropies.min
end

#is_strong?(min_entropy: BASE_ENTROPY, use_dictionary: false, min_word_length: 4, extra_dictionary_words: []) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/strong_password/strength_checker.rb', line 18

def is_strong?(min_entropy: BASE_ENTROPY, use_dictionary: false, min_word_length: 4, extra_dictionary_words: [])
  weak = (EntropyCalculator.calculate(base_password) < min_entropy) ||
         (EntropyCalculator.calculate(base_password.downcase) < min_entropy) ||
         (QwertyAdjuster.new(base_password).is_weak?(min_entropy: min_entropy))
  if !weak && use_dictionary
    return DictionaryAdjuster.new(base_password).is_strong?(min_entropy: min_entropy,
                min_word_length: min_word_length, 
                extra_dictionary_words: extra_dictionary_words)
  else
    return !weak
  end
end

#is_weak?(min_entropy: BASE_ENTROPY, use_dictionary: false, min_word_length: 4, extra_dictionary_words: []) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
# File 'lib/strong_password/strength_checker.rb', line 11

def is_weak?(min_entropy: BASE_ENTROPY, use_dictionary: false, min_word_length: 4, extra_dictionary_words: [])
  !is_strong?(min_entropy: min_entropy,
              use_dictionary: use_dictionary, 
              min_word_length: min_word_length, 
              extra_dictionary_words: extra_dictionary_words)
end