forked from devmanorg/6_password_strength
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_strength.py
54 lines (42 loc) · 1.48 KB
/
password_strength.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
import re
import getpass
import string
import os
def load_blacklist(filepath):
with open(filepath, 'r') as file_handler:
read_data = file_handler.read()
blacklist = re.findall(r"[\w']+", read_data)
return blacklist
def check_symbols(password):
score = sum([int(bool(re.search(r'[0-9]', password)) * 2),
int(bool(re.search(r'[a-z]', password)) * 2),
int(bool(re.search(r'[A-Z]', password)) * 2),
((any(char for char in password
if char in string.punctuation)) * 4)])
return score
def check_blacklist(password, blacklist):
for word in blacklist:
if password == word:
return -10
return 0
def check_length(password):
length_scores = len(password) / 2.0
max_length_scores = 10
if length_scores > max_length_scores:
length_scores = max_length_scores
return length_scores
def get_password_strength(password, blacklist):
total_scores = 0
total_scores += check_blacklist(password, blacklist)
total_scores += check_length(password)
total_scores += check_symbols(password)
if total_scores < 0:
total_scores = 0
return int(total_scores / 2.0)
if __name__ == '__main__':
if os.path.isfile('blacklist.txt'):
password = getpass.getpass('Пароль: ')
blacklist = load_blacklist('blacklist.txt')
print(get_password_strength(password, blacklist))
else:
print('Error: No such file in directory')