From 8c407c7b40ec917591d3153056974a1151302a2b Mon Sep 17 00:00:00 2001 From: Entity069 Date: Wed, 18 Dec 2024 16:50:48 +0530 Subject: [PATCH] Added json format saving to password files --- contributers.md | 1 - main.py | 39 +++++-------------------------- manager.py | 60 +++++++++++++++--------------------------------- requirements.txt | 1 - 4 files changed, 25 insertions(+), 76 deletions(-) delete mode 100644 contributers.md delete mode 100644 requirements.txt diff --git a/contributers.md b/contributers.md deleted file mode 100644 index cf40a7e..0000000 --- a/contributers.md +++ /dev/null @@ -1 +0,0 @@ -Tanmay Arya \ No newline at end of file diff --git a/main.py b/main.py index 91a81e5..f1a0cdc 100644 --- a/main.py +++ b/main.py @@ -1,15 +1,6 @@ from manager import PasswordManager -import pyperclip - - -def validate_key_loaded(pm : PasswordManager): - if not pm.keyloaded: - print("Key not loaded. Please load a key first.") - return False - return True - def main(): password = { "gmail": "password1", @@ -26,7 +17,6 @@ def main(): 4. Load an existing password file 5. Add a password 6. Get a password - 7. List all sites q. Quit """) @@ -39,36 +29,19 @@ def main(): elif choice == '2': path = input("Enter key file path: ").strip() pm.load_key(path) - elif choice == '3' and validate_key_loaded(pm): + elif choice == '3': path = input("Enter password file path: ").strip() pm.create_password_file(path, password) - elif choice == '4' and validate_key_loaded(pm): + elif choice == '4': path = input("Enter password file path: ").strip() pm.load_password_file(path) - elif choice == '5' and validate_key_loaded(pm): + elif choice == '5': site = input("Enter site: ").strip() password = input("Enter password: ").strip() - if pm.validate_strength(password): - print("added successfully") - else: - print("WARNING: This password is weak, It is recommended to set a stronger password") - print("- Password should be more than 8 characters long") - print("- Password should have alphanumeric characters, capital letters and special characters") pm.add_password(site, password) - - elif choice == '6' and validate_key_loaded(pm): - + elif choice == '6': site = input("Enter site: ").strip() - res = pm.get_password(site) - print(f"Password for {site}: {res}") - if(res != "Password not found."): - pyperclip.copy(pm.get_password(site)) - print("Password copied to clipboard.") - - elif choice == '7': - print("Saved Sites:") - for site in pm.password_dict: - print(site) + print(f"Password for {site}: {pm.get_password(site)}") elif choice == 'q': done = True print("Goodbye!") @@ -77,4 +50,4 @@ def main(): if __name__ == '__main__': - main() + main() \ No newline at end of file diff --git a/manager.py b/manager.py index 01ffafd..bc3d250 100644 --- a/manager.py +++ b/manager.py @@ -1,5 +1,5 @@ from cryptography.fernet import Fernet - +import json class PasswordManager: @@ -7,64 +7,42 @@ def __init__(self): self.key = None self.password_file = None self.password_dict = {} - self.keyloaded = False def create_key(self, path): self.key = Fernet.generate_key() with open(path, 'wb') as f: f.write(self.key) - self.keyloaded = True def load_key(self, path): with open(path, 'rb') as f: self.key = f.read() - self.keyloaded = True - def create_password_file(self, path, initial_values=None): self.password_file = path if initial_values is not None: - for site in initial_values: - print(initial_values[site]) - self.add_password(site, initial_values[site]) + 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() + try: + with open(path, 'r') as f: + data = json.load(f) + for site, encrypted in data.items(): + decrypted = Fernet(self.key).decrypt(encrypted.encode()).decode() + self.password_dict[site] = decrypted + except FileNotFoundError: + print(f"Password file {path} not found.") + except json.JSONDecodeError: + print(f"Error reading the password file {path}. It may be corrupted.") def add_password(self, site, password): - self.password_dict[site] = password + encrypted = Fernet(self.key).encrypt(password.encode()).decode() + self.password_dict[site] = encrypted 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") + with open(self.password_file, 'w') as f: + json.dump(self.password_dict, f, indent=4) + def get_password(self, site): - return self.password_dict.get(site, "Password not found.") - def validate_strength(self, password): - # a password is strong if it has length greater than 8 - # it has special characters such as !@#$%^&* - # it is a mix of letters, numbers - SpecialChar = '!@#$%^&*' - has_good_length = False - has_special_char = False - has_numeric_characters = False - has_capital_letters = False - has_small_letters = False - if len(password) > 8: - has_good_length = True - for chr in password: - if chr in SpecialChar: - has_special_char = True - if chr.isupper(): - has_capital_letters = True - if chr.islower(): - has_small_letters = True - if chr.isdigit(): - has_numeric_characters = True - return has_numeric_characters and has_good_length and\ - has_capital_letters and has_special_char and has_small_letters - + return self.password_dict.get(site, "Password not found.") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 2069479..0000000 --- a/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -pyperclip \ No newline at end of file