Skip to content

Commit f7e6e47

Browse files
authored
Merge pull request #141 from aryanadla/main
Added customizable password options, strength indicator, and file saving functionality.
2 parents 9117876 + 8e82538 commit f7e6e47

File tree

1 file changed

+47
-12
lines changed

1 file changed

+47
-12
lines changed

Password_Generator/Password_Generator.py

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
import string
33
import secrets
44

5-
def generate_password(length=8):
5+
def generate_password(length=8, include_digits=True, include_special=True):
66
if length < 8:
77
raise ValueError("Password length should be at least 8 characters for security.")
88

99
lower = string.ascii_lowercase
1010
upper = string.ascii_uppercase
11-
digits = string.digits
12-
special = "!@#$%^&*()_+-=[]{}|;:,.<>?"
11+
digits = string.digits if include_digits else ""
12+
special = "!@#$%^&*()_+-=[]{}|;:,.<>?" if include_special else ""
1313

1414
all_chars = lower + upper + digits + special
1515

1616
while True:
1717
password = ''.join(secrets.choice(all_chars) for _ in range(length))
1818
if (any(c.islower() for c in password)
1919
and any(c.isupper() for c in password)
20-
and sum(c.isdigit() for c in password) >= 2
21-
and sum(c in special for c in password) >= 1):
20+
and (sum(c.isdigit() for c in password) >= 2 if include_digits else True)
21+
and (sum(c in special for c in password) >= 1 if include_special else True)):
2222
return password
2323

2424
def strengthen_password(base_password):
@@ -44,16 +44,51 @@ def strengthen_password(base_password):
4444
random.shuffle(password)
4545
return ''.join(password)
4646

47+
def password_strength(password):
48+
strength = 0
49+
if len(password) >= 12:
50+
strength += 1
51+
if any(c.islower() for c in password):
52+
strength += 1
53+
if any(c.isupper() for c in password):
54+
strength += 1
55+
if any(c.isdigit() for c in password):
56+
strength += 1
57+
if any(c in "!@#$%^&*()_+-=[]{}|;:,.<>?" for c in password):
58+
strength += 1
59+
return strength
60+
61+
def save_password_to_file(password):
62+
with open("generated_passwords.txt", "a") as f:
63+
f.write(password + "\n")
64+
4765
if __name__ == "__main__":
48-
user_input = input("Enter desired password length (at least 8) or a base password: ")
66+
choice = input("Do you want to (g)enerate a new password or (s)trengthen an existing password? (g/s): ").strip().lower()
4967

50-
try:
51-
password_length = int(user_input)
52-
strong_password = generate_password(password_length)
53-
print(f"Your generated password is: {strong_password}")
54-
except ValueError:
68+
if choice == 'g':
69+
user_input = input("Enter desired password length (at least 8): ")
70+
include_digits = input("Include digits? (y/n): ").strip().lower() == 'y'
71+
include_special = input("Include special characters? (y/n): ").strip().lower() == 'y'
72+
73+
try:
74+
password_length = int(user_input)
75+
strong_password = generate_password(password_length, include_digits, include_special)
76+
print(f"Your generated password is: {strong_password}")
77+
print(f"Password Strength: {password_strength(strong_password)}/5")
78+
save_password_to_file(strong_password)
79+
except ValueError as e:
80+
print(f"Error: {e}")
81+
82+
elif choice == 's':
83+
user_input = input("Enter a base password to strengthen: ")
84+
5585
try:
5686
strengthened_password = strengthen_password(user_input)
5787
print(f"Your strengthened password is: {strengthened_password}")
88+
print(f"Password Strength: {password_strength(strengthened_password)}/5")
89+
save_password_to_file(strengthened_password)
5890
except ValueError as e:
59-
print(f"Error: {e}")
91+
print(f"Error: {e}")
92+
93+
else:
94+
print("Invalid choice. Please enter 'g' or 's'.")

0 commit comments

Comments
 (0)