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

Adds hierarchical password storage to the manager #83

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ Visit the **python** channel and ping `2Y` for assistance.
- `3`: Create a new password file.
- `4`: Load an existing password file.
- `5`: Add a new password to the file.
- `6`: Retrieve a password from the file.
- `6`: Retrieve a password from the file.
- `11`: Add a hierarchy heading in password_file
- `12`: Access any hierarchy in the password_file to add password within it
- `q`: Quit the application.

---
Expand Down
11 changes: 9 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def main():
5. Add a password
6. Get a password
7. List all sites
11. Add a folder
12. Access a folder
q. Quit
""")

Expand Down Expand Up @@ -57,14 +59,19 @@ def main():
pm.add_password(site, password)

elif choice == '6' and validate_key_loaded(pm):

site = input("Enter site: ").strip()
print(f"Password for {site}: {pm.get_password(site)}")
elif choice == '11':
name = input("Enter folder name: ").strip()
pm.add_folder(name)
elif choice == '12':
path = input("Enter folder path: ").strip()
pm.access_folder(path)
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:
Expand Down
67 changes: 61 additions & 6 deletions manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ class PasswordManager:
def __init__(self):
self.key = None
self.password_file = None
self.numberoffolders=-1
self.currentfolder=0
self.folder=[]
self.password_dict = {}
self.folder_dict={}
isFolder=False
self.keyloaded = False

def create_key(self, path):
Expand All @@ -32,18 +37,68 @@ 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()

site, encrypted = line.strip().split(":")
if(site=="folder"):
self.folder.append(encrypted)
self.numberoffolders=self.numberoffolders+1
self.currentfolder=self.numberoffolders
self.folder_dict[encrypted]=0
isFolder=True
elif(isFolder):
self.password_dict[site] = Fernet(self.key).decrypt(encrypted.encode()).decode()
self.folder_dict[self.folder[self.numberoffolders]]=self.folder_dict[self.folder[self.numberoffolders]]+1
else:
self.password_dict[site] = Fernet(self.key).decrypt(encrypted.encode()).decode()
def add_folder(self, name):
self.folder.append(name)
self.numberoffolders=self.numberoffolders+1
if self.password_file is not None:
with open(self.password_file, 'a+') as f:
encrypted = name
f.write(f"folder:{encrypted}\n")
self.currentfolder=self.numberoffolders
self.folder_dict[name]=0
def access_folder(self, path):
t=0
for i in range (len(self.folder)):
if(self.folder[i]==path):
self.currentfolder=i
t=1
if(t==0):
print("folder not found")

def add_password(self, site, password):
if site in self.password_dict:
print(f"Warning: A password for the site '{site}' already exists.")
return
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")
if (self.currentfolder==self.numberoffolders):
with open(self.password_file, 'a+') as f:
encrypted = Fernet(self.key).encrypt(password.encode()).decode()
f.write(f"{site}:{encrypted}\n")
self.folder_dict[self.folder[self.numberoffolders]]=self.folder_dict[self.folder[self.numberoffolders]]+1
else:
with open(self.password_file, 'r') as f:
lines=f.readlines()
f.close()
with open(self.password_file, 'w') as f:
count=0
l=0
for k in range(self.currentfolder+1):
l+=self.folder_dict[self.folder[k]]
l+=1
for line in lines:
if(count<l):
f.write(line)
count+=1
elif(count==l):
encrypted = Fernet(self.key).encrypt(password.encode()).decode()
f.write(f"{site}:{encrypted}\n")
count+=1
f.write(line)
else :
f.write(line)

def get_password(self, site):
return self.password_dict.get(site, "Password not found.")
Expand Down