-
Notifications
You must be signed in to change notification settings - Fork 0
/
password_meter.py
420 lines (359 loc) · 15.1 KB
/
password_meter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
"""
Password Meter:github.com/Layto888/Password-meter-py
Compute passwords strength and find the best password.
Rate your password or generate a strong one.
Disclaimer:
This application is designed to assess the strength of password strings.
The instantaneous visual feedback provides the user a means to improve
the strength of their passwords, with a hard focus on breaking the
typical bad habits of faulty password formulation.
Since no official weighting system exists, we created our own formulas
to assess the overall strength of a given password.
Please note, that this application does not utilize the typical "days-to-crack"
approach for strength determination.
We have found that particular system to be severely lacking and unreliable
for real-world scenarios. This application is neither perfect nor foolproof,
and should only be utilized as a loose guide in determining methods for
improving the password creation process.
Nota bene: The program is inspired from :
http://www.passwordmeter.com/ with different approach and proper code.
Features:
- A simple to use API for testing the strength of your password.
- Generating strong passwords
- suggest improvement in case of weak password.
Usage example 1:
>>> from password_meter import Password
>>> password = Password('Azerty22')
>>> password.rate()
Usage example 2:
>>> from password_meter import Password
>>> from constants import *
>>> Password().find(8, display=False, spec=USE_PUNCTATIONS+USE_DIGITS)
# or this will return a new safe password and its score:
>>> my_password, my_score = Password().find(8)
"""
import string
import random
import logging
from constants import *
from matplotlib import pyplot as plt
__author__ = 'A.Amine'
__version__ = '0.5'
logger = logging.getLogger('password')
logging.basicConfig(level=logging.WARNING)
class Password(object):
""" Rate your password or generate a strong one """
# test array avoid including string.whitespace
# when generating random passwords.
str_array = string.ascii_letters
pnc_array = string.punctuation
dgt_array = string.digits
global_array = str_array + pnc_array + dgt_array
def __init__(self, password=''):
self.password = password
self.len = 0
self.nupper = 0
self.nlower = 0
self.ndigit = 0
self.symbol = 0
self.requirement = 0
self.score = 0.0 # limited [0% - 100%]
self.super_score = 0.0 # the real score - 100 %
self.requirement_factor = 0
self._get_infos()
# For drawings list of scores / passowrds length.
self.scores_list = []
self.tentatives_counter = []
def find(self, length, display=True, spec=ALL, msg=''):
""" This function is to call if yo want find the best password
with (length l):
the idea is to generate MAX_TEST of passwords and find the best score.
return the safest password.
use spec to specify if you want to includes: letters, digits, symbols.
"""
assert (length >= MIN_PASSWORD_LENGTH), 'Insufficient length for a password'
assert (length <= MAX_PASSWORD_LENGTH), 'Length too large for a password'
# tentatives counter
test_counter = 1
# check for spec
if spec > ALL or spec < USE_LETTERS:
logger.error(
'\nThis specification is not allowed. -> specs are set to default now.\n')
spec = ALL
best_password = Password()
for _ in range(MAX_TEST):
new_pass = self._random_password(length, spec)
new_pass._global_score()
if new_pass.score > best_password.score:
best_password = new_pass
# set drawable list
self.scores_list.append(new_pass.score)
self.tentatives_counter.append(test_counter)
if display:
new_pass._show_little_summary()
test_counter += 1
# show the best pass found
if display:
best_password._show_summary()
# draw the evolution of password's score
plt.plot(self.tentatives_counter, self.scores_list, label=msg, marker='.')
return best_password.password, best_password.score
def rate(self):
"""
This is the function to call if you want rate your password.
return: the score value.
"""
self._global_score()
self._show_summary()
self._suggest_improvement()
return self.score
def drawGraph(self):
"""
Represent the evolution password's complexity within a graph.
"""
plt.xlabel('Number of tests')
plt.ylabel('Score (Password Strength)')
plt.title('Password complexity evolution')
plt.suptitle('Evolution of passwords score according to the generated passwords.')
plt.legend()
plt.grid(True)
plt.show()
def _suggest_improvement(self):
"""
If the score is under 50% it will be considered 'weak'
suggest then some improvement depending on the original password letters.
"""
if self.score < MAX_SCORE / 2.0:
additional_part, score = self.find(
MIN_LENGTH - 2, spec=USE_DIGITS + USE_PUNCTATIONS)
improved_password = self.password + additional_part
print('\nYour password is pretty weak, we suggest: {}'.format(
improved_password))
def __repr__(self):
return str(self.__dict__)
def _get_infos(self):
""" alors the fucntion compute lower/uper/digit...and check for default
minimum requirements by incrementing self.requirement for each
case bellow:
- Minimum MIN_LENGTH characters in length
- Contains 3/4 of the following items + the min length.:
- Uppercase Letters MIN_LOWUP
- Lowercase Letters MIN_LOWUP
- Numbers MIN_DIGITS
- Symbols MIN_PUNCT """
for letter in self.password:
if letter.isupper():
self.nupper += 1
elif letter.islower():
self.nlower += 1
elif letter.isdigit():
self.ndigit += 1
elif letter in string.punctuation:
self.symbol += 1
self.len = len(self.password)
if self.len > MIN_LENGTH:
self.requirement += 1
if self.ndigit > MIN_DIGITS:
self.requirement += 1
if self.nlower > MIN_LOWUP:
self.requirement += 1
if self.nupper > MIN_LOWUP:
self.requirement += 1
if self.symbol > MIN_PUNCT:
self.requirement += 1
logger.info('total self.requirement = {}'.format(self.requirement))
def _middle_ns(self):
""" compute the flat middle numbers or symbols in string. """
score = 0
for i, j in enumerate(self.password):
if i > 0 and i < self.len - 1:
if j.isdigit() or j in string.punctuation:
score += 1
return 2 * score
def _compute_addition(self):
""" A) addition part """
if self.len < MIN_LENGTH or self.requirement < MIN_REQUIREMENTS:
self.requirement_factor = 0 # KO
else:
self.requirement_factor = 1 # OK
# adding flat :
self.score += (self.requirement * 2) * \
self.requirement_factor + (self.len * 4) + (self.symbol * 6)
self.score += self._middle_ns()
# adding cond :
if self.nupper:
self.score += (self.len - self.nupper) * 2
if self.nlower:
self.score += (self.len - self.nlower) * 2
if self.ndigit:
self.score += self.ndigit * 4
def _compute_deduction(self):
""" B) compute the score of deduction part. """
self.score += self._only_letters()
self.score += self._only_digits()
self.score += self._repetitive_chars2()
self.score += self._consecutive_letter()
self.score += self._consecutive_digit()
self.score += self._check_sequential()
def _only_letters(self):
if (self.nlower + self.nupper) == self.len:
return -self.len
return 0
def _only_digits(self):
if self.ndigit == self.len:
return -self.len
return 0
def _repetitive_chars2(self):
""" Each time the number of repetition of some 'character' is >= MIN_REPCHAR
we add its square to the negative score """
counter = 0
score = 0
rep_array = []
for i in range(0, self.len - 1):
# check for ascending sequential characters & numbers
if self.password[i] == self.password[i + 1]:
counter += 1
else:
if counter >= MIN_REPCHAR:
rep_array.append(counter)
logger.info('add to rep_array: {}'.format(counter))
counter = 0
rep_array.append(counter)
for value in rep_array:
score += (value * value)
if score > MAX_SCORE:
score = MAX_SCORE
score = MAX_SCORE
return - score
def _consecutive_letter(self):
""" Consecutive Uppercase letters and lower (case insensitive)
we start the count after the 'MIN_CONSECHAR=2' consecutive
character in the string. """
score = 0
counter = 0
for letter in self.password:
if letter.isalpha():
counter += 1
if counter >= MIN_CONSECHAR:
score += 1
logger.info('consec letter {}:{} time >= {} '.format
(score, counter, MIN_CONSECHAR))
else:
counter = 0
return -2 * score
def _consecutive_digit(self):
""" Consecutive numbers, we start the count after the MIN_CONSEDIGIT
Nota bene: that functions consecutive_letter and consecutive_digit are
computed as a single score (the consecutive score value), combined as one
fucntion, we just split them for more readability.
"""
score = 0
counter = 0
for letter in self.password:
if letter.isdigit():
counter += 1
if counter >= MIN_CONSEDIGIT:
score += 1
logger.info('consec digit {} time >= {} '.format
(counter, MIN_CONSECHAR))
else:
counter = 0
return -2 * score
def _check_sequential(self):
""" check for ascending/descending sequential Letters & digits;
start when the counter is >= MIN_SEQUENTIAL """
score = 0
counter_asc_seq = counter_des_seq = 0
for i in range(0, self.len - 1):
# check for ascending sequential characters & numbers
if ord(self.password[i]) + 1 == ord(self.password[i + 1]):
counter_asc_seq += 1
if counter_asc_seq >= MIN_SEQUENTIAL:
score += 1
# check for descending sequential characters & numbers
elif ord(self.password[i]) == ord(self.password[i + 1]) + 1:
counter_des_seq += 1
if counter_des_seq >= MIN_SEQUENTIAL:
score += 1
else:
counter_asc_seq = counter_des_seq = 0
return -3 * score
def _global_score(self):
"""
Compute the final score = addition score A + deduction score B
(see: functions part A / part B).
"""
self._compute_addition()
self._compute_deduction()
self.super_score = self.score - 100
# if self.score > MAX_SCORE:
# self.score = MAX_SCORE
# elif self.score < MIN_SCORE:
# self.score = MIN_SCORE
def _show_little_summary(self):
""" Display password and its score each time we find a new
best password"""
print('Best password found: {} Global score {} % (Ratio {}) '.format
(
self.password, self.score, self.super_score)
)
@staticmethod
def _random_password(length, spec=ALL):
"""generate new Password instance with a random word:
len is the length.
"""
if spec == ALL:
list_char = Password.global_array
elif spec == USE_LETTERS:
list_char = Password.str_array
elif spec == USE_DIGITS:
list_char = Password.dgt_array
elif spec == USE_PUNCTATIONS:
list_char = Password.pnc_array
elif spec == USE_LETTERS + USE_DIGITS:
list_char = Password.str_array + Password.dgt_array
elif spec == USE_LETTERS + USE_PUNCTATIONS:
list_char = Password.str_array + Password.pnc_array
elif spec == USE_DIGITS + USE_PUNCTATIONS:
list_char = Password.pnc_array + Password.dgt_array
word = ''
for _ in range(length):
word += random.choice(list_char)
return Password(word)
def _show_summary(self):
""" Display all stuffs about the password """
print('\n========= Summary ========= \n')
print('password: {} \n'.format(self.password))
print('length: ({}) Bonus {}'.format(self.len, self.len * 4))
if self.nupper > 0:
print('upper letters: ({}) Bonus {}'.format
(
self.nupper, (self.len - self.nupper) * 2)
)
else:
print('upper letters: ({}) Bonus {}'.format(self.nupper, 0))
if self.nlower > 0:
print('lower letters: ({}) Bonus {}'.format
(
self.nlower, (self.len - self.nlower) * 2)
)
else:
print('lower letters: ({}) Bonus {}'.format(self.nlower, 0))
print('numbers: ({}) Bonus {}'.format(self.ndigit, self.ndigit * 4))
print('symbols: ({}) Bonus {}'.format(self.symbol, self.symbol * 6))
print('middle num/symb bonus: {}'.format(self._middle_ns()))
print('requierements: ({}/5) Bonus {}'.format
(
self.requirement,
self.requirement * 2 * self.requirement_factor)
)
print('only letters bonus: {}'.format(self._only_letters()))
print('only digits bonus: {}'.format(self._only_digits()))
print('repeat chars bonus: {}'.format(self._repetitive_chars2()))
print('consecutive letters: {}'.format(self._consecutive_letter()))
print('consecutive digits: {}'.format(self._consecutive_digit()))
print('sequential (Letters/Digits/Symbols) bonus: {}'.format
(
self._check_sequential())
)
print('Your global score: {} %'.format(self.score))