forked from Grow-with-Open-Source/Python-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword_manager_cli.py
More file actions
114 lines (91 loc) · 4.16 KB
/
Copy pathpassword_manager_cli.py
File metadata and controls
114 lines (91 loc) · 4.16 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import random, datetime, base64, argparse, os
from cryptography.fernet import Fernet
# 🔑 File where secret key will be stored
KEY_FILE = "secret.key"
# ------------------------------------------------------
# STEP 1: Generate a new encryption key (only once))
def generate_key():
key = Fernet.generate_key()
with open(KEY_FILE, "wb") as f:
f.write(key)
# STEP 2: Load the ecryption key
def load_key():
if not os.path.exists(KEY_FILE):
print("No Key found. Creating a new one...")
generate_key()
with open(KEY_FILE, "rb") as f:
return f.read()
# STEP 3: Generate password based on user settings
def generate_password(length, use_lower, use_upper, use_digits, use_specials):
lowercase = 'abcdefghijklmnopqrstuvwxyz'
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
digits = '0123456789'
specials = '!@#$%^&*()'
# Build character set based on user input
character_set = ''
if use_lower:
character_set = character_set + lowercase
if use_upper:
character_set = character_set + uppercase
if use_digits:
character_set = character_set + digits
if use_specials:
character_set = character_set + specials
if not character_set:
return "Error: No character sets selected. Cannot generate password."
password = ''
for i in range(length):
password = password + random.choice(character_set)
return password
# STEP 4: Check password strength
def check_strength(length, use_lower, use_upper, use_digits, use_specials):
score = 0
# Add points for character variety
score = score + use_lower + use_upper + use_digits + use_specials
if length >= 12:
score = score + 1
if score <= 2:
return "Weak"
elif score == 3 or score == 4:
return "Medium"
else:
return "Strong"
# STEP 5: Command-line interface using argparse
def main():
parser = argparse.ArgumentParser(description="🔐 Password Generator Tool")
parser.add_argument('--length', type=int, required=True, help='Password length (e.g., 8, 12, 16)')
parser.add_argument('--label', type=str, required=True, help='Purpose or label for the password (e.g, Google, Gmail)')
parser.add_argument('--lower', action='store_true', help='Include lowercase letters')
parser.add_argument('--upper', action='store_true', help='Include uppercase letters')
parser.add_argument('--digits', action='store_true', help='Include digits')
parser.add_argument('--specials', action='store_true', help='Include special characters')
args = parser.parse_args()
# Validate length
if args.length <= 0:
print("❌ Password length must be positive. Try again.")
return
# Generate and evaluate password
password = generate_password(args.length, args.lower, args.upper, args.digits, args.specials)
if password.startswith("Error"):
print(password)
return
print(f"✅ Your generated password is: {password}")
strength = check_strength(args.length, args.lower, args.upper, args.digits, args.specials)
print(f"💪 Password Strenght: {strength}")
# STEP 6: Encrypt password using Fernet
key = load_key()
fernet = Fernet(key)
encrypted_password = fernet.encrypt(password.encode()).decode() # This variable already holds the Fernet encrypted string.
# STEP 7: Save encrypted password to file
with open("saved_passwords.txt", "a") as file:
timestamp = datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S")
# The 'encoded_password' (base64 of original password) was being saved previously.
# We should save the 'encrypted_password' (Fernet encrypted string) instead.
file.write(f"\n[{timestamp}]\n")
file.write(f"Label: {args.label}\n")
file.write(f"Encrypted Password: {encrypted_password}\n") # Save the Fernet encrypted password and use "Encrypted Password:" label
file.write(f"Included - Lowercase: {args.lower}, Uppercase: {args.upper}, Digits: {args.digits}, Special Characters: {args.specials}\n")
file.write("-" * 40 + "\n")
print("🔒 Password encrypted and saved to 'saved_passwords.txt")
if __name__ == '__main__':
main()