Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added json format saving to password files (new) #107

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion contributers.md

This file was deleted.

39 changes: 6 additions & 33 deletions main.py
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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
""")

Expand All @@ -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!")
Expand All @@ -77,4 +50,4 @@ def main():


if __name__ == '__main__':
main()
main()
60 changes: 19 additions & 41 deletions manager.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,48 @@
from cryptography.fernet import Fernet

import json

class PasswordManager:

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.")
1 change: 0 additions & 1 deletion requirements.txt

This file was deleted.