Module: StrongPassword::EntropyCalculator

Defined in:
lib/strong_password/entropy_calculator.rb

Defined Under Namespace

Classes: EntropyResolver

Class Method Summary collapse

Class Method Details

.bits(password) ⇒ Object

The basic NIST entropy calculation is based solely on the length of the password in question.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/strong_password/entropy_calculator.rb', line 14

def self.bits(password)
  length = password.length
  bits = if length > 20
    4 + (7 * 2) + (12 * 1.5) + length - 20
  elsif length > 8
    4 + (7 * 2) + ((length - 8) * 1.5)
  elsif length > 1
    4 + ((length - 1) * 2)
  else
    (length == 1 ? 4 : 0)
  end
  bits + NistBonusBits.bonus_bits(password)
end

.bits_with_repeats_weakened(password) ⇒ Object

A modified version of the basic entropy calculation which lowers the amount of entropy gained for each repeated character in the password



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

def self.bits_with_repeats_weakened(password)
  resolver = EntropyResolver.new
  bits = password.chars.each.with_index.inject(0) do |result, (char, index)|
    char_value = resolver.entropy_for(char)
    result += bit_value_at_position(index, char_value)
  end
  bits + NistBonusBits.bonus_bits(password)
end

.calculate(password, repeats_weakened = true) ⇒ Object

Calculates NIST entropy for a password.



4
5
6
7
8
9
10
# File 'lib/strong_password/entropy_calculator.rb', line 4

def self.calculate(password, repeats_weakened = true)
  if repeats_weakened
    bits_with_repeats_weakened(password)
  else
    bits(password)
  end
end