Skip to content

Commit

Permalink
Merge pull request #1 from IMGIITRoorkee/main
Browse files Browse the repository at this point in the history
Fixes IMGIITRoorkee#110 Adding a Password Generator
  • Loading branch information
keertigola23244444 authored Dec 22, 2024
2 parents 075360e + 319276c commit bf1cb4c
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ A **CLI-based password manager** built in Python for secure password storage and
├── main.py # Entry point for executing the program
├── manager.py # Core logic and functionality
```
### Install required dependencies:
`pip install -r requirements.txt`

---

Expand Down
1 change: 1 addition & 0 deletions contributers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tanmay Arya
37 changes: 32 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
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 @@ -17,6 +26,7 @@ def main():
4. Load an existing password file
5. Add a password
6. Get a password
7. List all sites
q. Quit
""")

Expand All @@ -29,19 +39,36 @@ def main():
elif choice == '2':
path = input("Enter key file path: ").strip()
pm.load_key(path)
elif choice == '3':
elif choice == '3' and validate_key_loaded(pm):
path = input("Enter password file path: ").strip()
pm.create_password_file(path, password)
elif choice == '4':
elif choice == '4' and validate_key_loaded(pm):
path = input("Enter password file path: ").strip()
pm.load_password_file(path)
elif choice == '5':
elif choice == '5' and validate_key_loaded(pm):
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':

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

site = input("Enter site: ").strip()
print(f"Password for {site}: {pm.get_password(site)}")
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)
elif choice == 'q':
done = True
print("Goodbye!")
Expand Down
36 changes: 34 additions & 2 deletions manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@ 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, password in initial_values.items():
self.add_password(site, password)
for site in initial_values:
print(initial_values[site])
self.add_password(site, initial_values[site])

def load_password_file(self, path):
self.password_file = path
Expand All @@ -31,6 +36,9 @@ def load_password_file(self, path):
self.password_dict[site] = Fernet(self.key).decrypt(encrypted.encode()).decode()

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:
Expand All @@ -39,3 +47,27 @@ def add_password(self, site, password):

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

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pyperclip

0 comments on commit bf1cb4c

Please sign in to comment.