Module: Philiprehberger::Password::Strength
- Defined in:
- lib/philiprehberger/password/strength.rb
Constant Summary collapse
- LABELS =
{ 0 => :terrible, 1 => :weak, 2 => :fair, 3 => :strong, 4 => :excellent }.freeze
Class Method Summary collapse
Class Method Details
.compute(password) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/philiprehberger/password/strength.rb', line 14 def self.compute(password) pwd = password.to_s return { score: 0, label: :terrible, entropy: 0.0 } if pwd.empty? ent = entropy(pwd) s = if ent < 28 0 elsif ent < 36 1 elsif ent < 60 2 elsif ent < 80 3 else 4 end { score: s, label: LABELS[s], entropy: ent.round(2) } end |
.entropy(password) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/philiprehberger/password/strength.rb', line 35 def self.entropy(password) pwd = password.to_s return 0.0 if pwd.empty? pool = 0 pool += 26 if pwd.match?(/[a-z]/) pool += 26 if pwd.match?(/[A-Z]/) pool += 10 if pwd.match?(/\d/) pool += 33 if pwd.match?(/[^a-zA-Z\d]/) pwd.length * Math.log2([pool, 1].max) end |