Class: CipherBureau::PasswordMeter

Inherits:
Object
  • Object
show all
Defined in:
lib/cipher_bureau/password_meter.rb

Overview

Copyright © 2013 Dynamic Project Management AS Copyright © 2013 Knut I. Stenmark

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Constant Summary collapse

LETTERS =
('a'..'z').to_a.join
NUMBERS =
(0..9).to_a.join
SYMBOLS =
')!@#$%^&*()'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(password) ⇒ PasswordMeter

Returns a new instance of PasswordMeter.



29
30
31
# File 'lib/cipher_bureau/password_meter.rb', line 29

def initialize(password)
  self.password = password
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



28
29
30
# File 'lib/cipher_bureau/password_meter.rb', line 28

def length
  @length
end

#passwordObject

Returns the value of attribute password.



28
29
30
# File 'lib/cipher_bureau/password_meter.rb', line 28

def password
  @password
end

Class Method Details

.strength(password) ⇒ Object



39
40
41
# File 'lib/cipher_bureau/password_meter.rb', line 39

def self.strength(password)
  new(password).strength
end

Instance Method Details

#absolute_scoreObject



59
60
61
62
# File 'lib/cipher_bureau/password_meter.rb', line 59

def absolute_score
  #@absolute_score ||= 
  additions - deductions
end

#additionsObject



64
65
66
# File 'lib/cipher_bureau/password_meter.rb', line 64

def additions
  number_of_characters + uppercase_letters + lowercase_letters + numbers + symbols + middle_numbers_or_symbols + requirements
end

#consecutive(pattern) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/cipher_bureau/password_meter.rb', line 172

def consecutive(pattern)
  n = 0
  prev = nil
  password.chars do |c|
    m = c.match(pattern)
    n += 1 if m && prev
    prev = m
  end
  n * 2
end

#consecutive_lowercaseObject



164
165
166
# File 'lib/cipher_bureau/password_meter.rb', line 164

def consecutive_lowercase
  consecutive /[a-z]/
end

#consecutive_numbersObject



168
169
170
# File 'lib/cipher_bureau/password_meter.rb', line 168

def consecutive_numbers
  consecutive /[0-9]/
end

#consecutive_uppercaseObject



160
161
162
# File 'lib/cipher_bureau/password_meter.rb', line 160

def consecutive_uppercase
  consecutive /[A-Z]/
end

#deductionsObject



68
69
70
71
72
# File 'lib/cipher_bureau/password_meter.rb', line 68

def deductions
  letters_only + numbers_only + repeated_characters + 
    sequential_letters + sequential_numbers + sequential_symbols +
    consecutive_uppercase + consecutive_lowercase + consecutive_numbers
end

#letters_onlyObject

Deductions



110
111
112
113
# File 'lib/cipher_bureau/password_meter.rb', line 110

def letters_only
  n = password.gsub(/[^a-zA-Z]/, '\1').length
  n == length ? n : 0
end

#lowercase_lettersObject



82
83
84
85
# File 'lib/cipher_bureau/password_meter.rb', line 82

def lowercase_letters
  n = password.gsub(/[^a-z]/, '\1').length
  n > 0 ? (length - n) * 2 : 0
end

#middle_numbers_or_symbolsObject



93
94
95
# File 'lib/cipher_bureau/password_meter.rb', line 93

def middle_numbers_or_symbols
  length < 3 ? 0 : password[1...-1].gsub(/[a-zA-Z]/, '\1').length * 2
end

#number_of_charactersObject

Additions



75
76
77
# File 'lib/cipher_bureau/password_meter.rb', line 75

def number_of_characters
  password.length * 4
end

#numbersObject



86
87
88
89
# File 'lib/cipher_bureau/password_meter.rb', line 86

def numbers
  n = password.gsub(/[^0-9]/, '\1').length
  n < length ? n * 4 : 0
end

#numbers_onlyObject



115
116
117
118
# File 'lib/cipher_bureau/password_meter.rb', line 115

def numbers_only
  n = password.gsub(/[^0-9]/, '\1').length
  n == length ? n : 0
end

#repeated_charactersObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/cipher_bureau/password_meter.rb', line 120

def repeated_characters
  nc = ni = 0
  length.times do |a|
    ch_exists = false
    length.times do |b|
      if a != b && password[a] == password[b]
        ch_exists = true
        ni += (length.to_f / (b - a)).abs
      end
    end
    if ch_exists
      nc += 1
      unique = length - nc;
      ni = unique != 0 ? (ni/unique).ceil : ni.ceil
    end
  end
  ni
end

#requirementsObject



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/cipher_bureau/password_meter.rb', line 96

def requirements
  if length >= 8
    numbers = password.gsub(/[^0-9]/, '\1').length > 0 ? 1 : 0
    lowercase = password.gsub(/[^a-z]/, '\1').length > 0 ? 1 : 0
    uppercase = password.gsub(/[^A-Z]/, '\1').length > 0 ? 1 : 0
    symbols = password.gsub(/[a-zA-Z0-9_]/, '\1').length > 0 ? 1 : 0
    n = (numbers + lowercase + uppercase + symbols)
    n > 2 ? (n + 1) * 2 : 0 # add 2 for satisfying length
  else
    0
  end
end

#sequential(str, p = password) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/cipher_bureau/password_meter.rb', line 151

def sequential(str, p = password)
  n = 0
  (str.size - 3).times do |i|
    s = str[i..i + 2]
    n += 1 if p.include?( s) || p.include?( s.reverse) 
  end
  n * 3
end

#sequential_lettersObject



139
140
141
# File 'lib/cipher_bureau/password_meter.rb', line 139

def sequential_letters
  sequential LETTERS, password.downcase
end

#sequential_numbersObject



143
144
145
# File 'lib/cipher_bureau/password_meter.rb', line 143

def sequential_numbers
  sequential NUMBERS
end

#sequential_symbolsObject



147
148
149
# File 'lib/cipher_bureau/password_meter.rb', line 147

def sequential_symbols
  sequential SYMBOLS
end

#strengthObject



43
44
45
46
# File 'lib/cipher_bureau/password_meter.rb', line 43

def strength
  n = absolute_score  
  n > 100 ? 100 : (n < 0 ? 0 : n )
end

#symbolsObject



90
91
92
# File 'lib/cipher_bureau/password_meter.rb', line 90

def symbols
  password.gsub(/[a-zA-Z0-9_]/, '\1').length * 6
end

#uppercase_lettersObject



78
79
80
81
# File 'lib/cipher_bureau/password_meter.rb', line 78

def uppercase_letters
  n = password.gsub(/[^A-Z]/, '\1').length
  n > 0 ? (length - n) * 2 : 0
end

#verbose_strengthObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/cipher_bureau/password_meter.rb', line 48

def verbose_strength
  case strength
  when 0...20   then "Very Weak"
  when 20...40  then "Weak"
  when 40...60  then "Good"
  when 60...80  then "Strong"
  else
    "Very Strong"
  end
end