Class: StrongPassword::StrengthChecker

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

Constant Summary collapse

BASE_ENTROPY =
18
PASSWORD_LIMIT =
1_000
EXTRA_WORDS_LIMIT =
1_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(password) ⇒ StrengthChecker

Returns a new instance of StrengthChecker.



9
10
11
# File 'lib/strong_password/strength_checker.rb', line 9

def initialize(password)
  @base_password = password.dup[0...PASSWORD_LIMIT]
end

Instance Attribute Details

#base_passwordObject (readonly)

Returns the value of attribute base_password.



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

def base_password
  @base_password
end

Instance Method Details

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



33
34
35
36
37
38
# File 'lib/strong_password/strength_checker.rb', line 33

def calculate_entropy(use_dictionary: false, min_word_length: 4, extra_dictionary_words: [])
  extra_dictionary_words.collect! { |w| w[0...EXTRA_WORDS_LIMIT] }
  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)


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

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)


13
14
15
16
17
18
# File 'lib/strong_password/strength_checker.rb', line 13

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