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(min_entropy: BASE_ENTROPY, use_dictionary: false, min_word_length: 4, extra_dictionary_words: []) ⇒ StrengthChecker

Returns a new instance of StrengthChecker.



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

def initialize(min_entropy: BASE_ENTROPY, use_dictionary: false, min_word_length: 4, extra_dictionary_words: [])
  @min_entropy = min_entropy
  @use_dictionary = use_dictionary
  @min_word_length = min_word_length
  @extra_dictionary_words = extra_dictionary_words
end

Instance Attribute Details

#extra_dictionary_wordsObject (readonly)

Returns the value of attribute extra_dictionary_words.



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

def extra_dictionary_words
  @extra_dictionary_words
end

#min_entropyObject (readonly)

Returns the value of attribute min_entropy.



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

def min_entropy
  @min_entropy
end

#min_word_lengthObject (readonly)

Returns the value of attribute min_word_length.



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

def min_word_length
  @min_word_length
end

#use_dictionaryObject (readonly)

Returns the value of attribute use_dictionary.



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

def use_dictionary
  @use_dictionary
end

Instance Method Details

#calculate_entropy(password) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/strong_password/strength_checker.rb', line 32

def calculate_entropy(password)
  base_password = password.dup[0...PASSWORD_LIMIT]
  extra_dictionary_words.collect! { |w| w[0...EXTRA_WORDS_LIMIT] }
  entropies = [
    EntropyCalculator.calculate(base_password),
    EntropyCalculator.calculate(base_password.downcase),
    qwerty_adjuster.adjusted_entropy(base_password)
  ]
  entropies << dictionary_adjuster.adjusted_entropy(base_password) if use_dictionary
  entropies.min
end

#is_strong?(password) ⇒ Boolean

Returns:

  • (Boolean)


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

def is_strong?(password)
  base_password = password.dup[0...PASSWORD_LIMIT]
  weak = (EntropyCalculator.calculate(base_password) < min_entropy) ||
         (EntropyCalculator.calculate(base_password.downcase) < min_entropy) ||
         (qwerty_adjuster.is_weak?(base_password))
  if !weak && use_dictionary
    return dictionary_adjuster.is_strong?(base_password)
  else
    return !weak
  end
end

#is_weak?(password) ⇒ Boolean

Returns:

  • (Boolean)


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

def is_weak?(password)
  !is_strong?(password)
end