-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
164 lines (128 loc) · 3.88 KB
/
main.py
File metadata and controls
164 lines (128 loc) · 3.88 KB
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
import random
import json
import cryptography.fernet
import utils
from cryptography.fernet import Fernet
import cryptography
LOWER = 'abcdefghijklmnopqrstuvwxyz'
UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
DIGITS = '0123456789'
PUNCTUATION = r"""!"#$%&'()*+,-.:;<=>?@^_`{|}~"""
def generate_key():
key = Fernet.generate_key()
with open("secret.key", "wb") as key_file:
key_file.write(key)
def load_key():
return open("secret.key", "rb").read()
try:
key = load_key()
KEY = key
except FileNotFoundError:
generate_key()
def encrypt_password(password: str, key: bytes) -> str:
f = Fernet(key)
encrypted_password = f.encrypt(password.encode())
return encrypted_password.decode()
def decrypt_password(encrypted_password: str, key: bytes) -> str:
f = Fernet(key)
try:
decrypted_password = f.decrypt(encrypted_password.encode())
return decrypted_password.decode()
except cryptography.fernet.InvalidToken:
return "Invalid Token"
def generate_password(length: int, anzahl: int, chosen_characters: str):
passwords = []
characters = ""
if "1" in chosen_characters:
characters += LOWER + UPPER # noqa
if "2" in chosen_characters:
characters += DIGITS
if "3" in chosen_characters:
characters += PUNCTUATION
for i in range(anzahl):
password = ''.join(random.choice(characters) for _ in range(length))
passwords.append(password)
return passwords
def rate_password(password: str):
score = 0
for i in password:
if i in LOWER:
score += 1
break
for i in password:
if i in UPPER:
score += 1
break
for i in password:
if i in DIGITS:
score += 2
break
for i in password:
if i in PUNCTUATION:
score += 2
break
if len(password) > 7:
score += 2
if len(password) > 15:
score += 3
# Compares with unsafe word file
unsafewords = []
with open('unsafewords.txt', "r") as file:
for i in file.readlines():
unsafewords.append(i)
if password in unsafewords:
return "Schwach"
if check_same_symbols(password, 3):
score -= 4
elif check_same_symbols(password, 2):
score -= 2
# Return with score
if score > 10:
return "Stark"
elif score >= 7:
return "Mittelstark"
else:
return "Schwach"
def check_same_symbols(password: str, same_symbol_count: int) -> bool:
if same_symbol_count > 1:
for i in range(len(password) - same_symbol_count + 1):
if password[i:i+same_symbol_count] == password[i] * same_symbol_count: # noqa
return True
return False
def save_password(name: str, username: str, password: str):
key = load_key()
encrypted_password = encrypt_password(password, key)
data = {
"name": name,
"username": username,
"password": encrypted_password
}
try:
with open('passwords.json', 'r') as file:
passwords = json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
passwords = []
passwords.append(data)
with open('passwords.json', 'w') as file:
json.dump(passwords, file, indent=4)
def get_passwords():
key = load_key()
try:
with open('passwords.json', 'r') as file:
passwords = json.load(file)
if not passwords:
return False
else:
for entry in passwords:
entry['password'] = decrypt_password(entry['password'], key) # noqa
return passwords
except (FileNotFoundError, json.JSONDecodeError):
return False
def main():
utils.Windows().main_menu
if __name__ == "__main__":
try:
load_key()
except FileNotFoundError:
generate_key()
main()