-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_manager.py
54 lines (41 loc) · 1.42 KB
/
password_manager.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
from cryptography.fernet import Fernet
import os
import shutil
def write_key():
if os.path.isfile("../MiscFiles/key.key") is False:
written_key = Fernet.generate_key()
with open("../MiscFiles/key.key", "wb") as key_file:
# noinspection PyUnboundLocalVariable
key_file.write(written_key)
def load_key():
if os.path.isfile("../MiscFiles/key.key") is False:
write_key()
file = open("../MiscFiles/key.key", "rb")
loaded_key = file.read()
file.close()
return loaded_key
def option_view():
with open("../MiscFiles/passwords.txt", "r") as f:
for line in f.readlines():
data = line.rstrip()
# noinspection SpellCheckingInspection
user, passw = data.split("|")
print(f"Username: {user} | Password:", fer.decrypt(passw.encode()).decode())
def option_add():
username = input("Account username: ")
pwd = input("Account password: ")
with open("../MiscFiles/passwords.txt", "a") as f:
f.write(username + "|" + fer.encrypt(pwd.encode()).decode() + "\n")
key = load_key()
fer = Fernet(key)
while True:
mode = input("Would you like to 'add' a new password, or 'view' stored passwords? Press 'Q' to quit...\n").lower()
if mode == "q":
break
if mode == "add":
option_add()
elif mode == "view":
option_view()
else:
print("No such option exists...")
continue