2
2
import string
3
3
import secrets
4
4
5
- def generate_password (length = 8 ):
5
+ def generate_password (length = 8 , include_digits = True , include_special = True ):
6
6
if length < 8 :
7
7
raise ValueError ("Password length should be at least 8 characters for security." )
8
8
9
9
lower = string .ascii_lowercase
10
10
upper = string .ascii_uppercase
11
- digits = string .digits
12
- special = "!@#$%^&*()_+-=[]{}|;:,.<>?"
11
+ digits = string .digits if include_digits else ""
12
+ special = "!@#$%^&*()_+-=[]{}|;:,.<>?" if include_special else ""
13
13
14
14
all_chars = lower + upper + digits + special
15
15
16
16
while True :
17
17
password = '' .join (secrets .choice (all_chars ) for _ in range (length ))
18
18
if (any (c .islower () for c in password )
19
19
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 ) ):
22
22
return password
23
23
24
24
def strengthen_password (base_password ):
@@ -44,16 +44,51 @@ def strengthen_password(base_password):
44
44
random .shuffle (password )
45
45
return '' .join (password )
46
46
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
+
47
65
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 ( )
49
67
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
+
55
85
try :
56
86
strengthened_password = strengthen_password (user_input )
57
87
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 )
58
90
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