-
Notifications
You must be signed in to change notification settings - Fork 0
/
password_manager.py
54 lines (44 loc) 路 1.75 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
''' def write_key():
key = Fernet.generate_key()
# key file, 'wb' = write bytes mode
# going to open a file, create the file key.key in 'wb' and write the key generated by Fernet
with open("key.key", "wb") as key_file:
key_file.write(key) '''
def load_key():
file = open("key.key", "rb")
key = file.read()
#TODO Remember to close files always
file.close()
return key
key = load_key()
# kind of initializing, load Fernet and pass it the key
fer = Fernet(key)
def view():
with open('passwords.txt', 'r') as f:
for line in f.readlines():
data = line.rstrip()
# '.split' will split everytime it finds the "|" character in line
# "hello|tim" into a list ["hello", "tim"]
# user, passw = puts the first index in the user var, and the 2nd index in passw var
user, passw = data.split("|")
print("User:", user, "| Password:",
fer.decrypt(passw.encode()).decode())
def add():
name = input('Account name: ')
pwd = input('Password: ')
# using 'with' to automatically close the file instead of manually using 'file'.close()
# open('name.txt', [mode= w(rite) or override if exists, r(ead), a(ppend), most flexible, add something if exists or create a new file if doesn't exist])
with open('passwords.txt', 'a') as f:
f.write(name + "|" + fer.encrypt(pwd.encode()).decode() + "\n")
while True:
mode = input("Would you like to add a new password or view existing ones (view, add), press q to quit? ").lower()
if mode == "q":
break
if mode == "view":
view()
elif mode == "add":
add()
else:
print("Invalid mode.")
continue