forked from IMGIITRoorkee/PwManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.py
87 lines (75 loc) · 3.18 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
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
from cryptography.fernet import Fernet
class PasswordManager:
"""
A class to manage passwords securely using encryption.
"""
def __init__(self):
"""
Initializes the manager with no key, no file, and an empty password list.
"""
self.key = None # No encryption key initially
self.password_file = None # No file for passwords yet
self.password_dict = {} # Empty dictionary to store passwords
def create_key(self, path):
"""
Creates and saves a new encryption key to a file.
Args:
path (str): The path to save the key.
"""
self.key = Fernet.generate_key() # Generate a new key
with open(path, 'wb') as f:
f.write(self.key) # Save the key to the file
def load_key(self, path):
"""
Loads an existing encryption key from a file.
Args:
path (str): The path to the key file.
"""
with open(path, 'rb') as f:
self.key = f.read() # Load the key from the file
def create_password_file(self, path, initial_values=None):
"""
Creates a password file and adds initial passwords if given.
Args:
path (str): The path for the password file.
initial_values (dict, optional): Initial site-password pairs to add.
"""
self.password_file = path # Set the file path
if initial_values is not None:
for site, password in initial_values.items():
self.add_password(site, password) # Add initial passwords
def load_password_file(self, path):
"""
Loads passwords from a file and decrypts them.
Args:
path (str): The password file path.
"""
self.password_file = path # Set the file path
with open(path, 'r') as f:
for line in f:
site, encrypted = line.strip().split(":") # Split the site and encrypted password
self.password_dict[site] = Fernet(self.key).decrypt(encrypted.encode()).decode() # Decrypt and store the password
def add_password(self, site, password):
"""
Adds a new password for a site. Warns if the site already exists.
Args:
site (str): The site name.
password (str): The password for the site.
"""
if site in self.password_dict:
print(f"Warning: '{site}' already has a password.") # Warn if site already exists
return # Do not add the password again
self.password_dict[site] = password # Add the new password
if self.password_file is not None:
with open(self.password_file, 'a+') as f:
encrypted = Fernet(self.key).encrypt(password.encode()).decode() # Encrypt the password
f.write(f"{site}:{encrypted}\n") # Save the encrypted password to the file
def get_password(self, site):
"""
Gets the password for a site.
Args:
site (str): The site name.
Returns:
str: The password or a message if not found.
"""
return self.password_dict.get(site, "Password not found.") # Return password or 'not found'