forked from IMGIITRoorkee/PwManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.py
44 lines (32 loc) · 1.36 KB
/
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
from cryptography.fernet import Fernet
class PasswordManager:
def __init__(self):
self.key = None
self.password_file = None
self.password_dict = {}
def create_key(self, path):
self.key = Fernet.generate_key()
with open(path, 'wb') as f:
f.write(self.key)
def load_key(self, path):
with open(path, 'rb') as f:
self.key = f.read()
def create_password_file(self, path, initial_values=None):
self.password_file = path
if initial_values is not None:
for site, password in initial_values.items():
self.add_password(site, password)
def load_password_file(self, path):
self.password_file = path
with open(path, 'r') as f:
for line in f:
site, encrypted = line.split(":")
self.password_dict[site] = Fernet(self.key).decrypt(encrypted.encode()).decode()
def add_password(self, site, password):
self.password_dict[site] = password
if self.password_file is not None:
with open(self.password_file, 'a+') as f:
encrypted = Fernet(self.key).encrypt(password.encode()).decode()
f.write(f"{site}:{encrypted}\n")
def get_password(self, site):
return self.password_dict.get(site, "Password not found.")