1- import random
1+ import secrets
2+ import string
23
3- def generatePassword (pwlength ):
4-
5- alphabet = "abcdefghijklmnopqrstuvwxyz"
6-
7- passwords = []
8-
9- for i in pwlength :
10-
11- password = ""
12- for j in range (i ):
13- next_letter_index = random .randrange (len (alphabet ))
14- password = password + alphabet [next_letter_index ]
15-
16- password = replaceWithNumber (password )
17- password = replaceWithUppercaseLetter (password )
18-
19- passwords .append (password )
4+ def generate_password (length : int = 12 , digits : bool = True , symbols : bool = True ) -> str :
205
21- return passwords
22-
23-
24- def replaceWithNumber (pword ):
25- for i in range (random .randrange (1 ,3 )):
26- replace_index = random .randrange (len (pword )// 2 )
27- pword = pword [0 :replace_index ] + str (random .randrange (10 )) + pword [replace_index + 1 :]
28- return pword
29-
30-
31- def replaceWithUppercaseLetter (pword ):
32- for i in range (random .randrange (1 ,3 )):
33- replace_index = random .randrange (len (pword )// 2 ,len (pword ))
34- pword = pword [0 :replace_index ] + pword [replace_index ].upper () + pword [replace_index + 1 :]
35- return pword
36-
37-
38-
39- def main ():
6+ min_required = int (digits ) + int (symbols )
7+ if length < min_required :
8+ raise ValueError ("Password length too small for selected options" )
409
41- numPasswords = int (input ("How many passwords do you want to generate? " ))
10+ pool = string .ascii_letters
11+ required_chars = []
4212
43- print ("Generating " + str (numPasswords )+ " passwords" )
44-
45- passwordLengths = []
46-
47- print ("Minimum length of password should be 3" )
13+ if digits :
14+ pool += string .digits
15+ required_chars .append (secrets .choice (string .digits ))
4816
49- for i in range (numPasswords ):
50- length = int (input ("Enter the length of Password #" + str (i + 1 ) + " " ))
51- if length < 3 :
52- length = 3
53- passwordLengths .append (length )
17+ if symbols :
18+ pool += string .punctuation
19+ required_chars .append (secrets .choice (string .punctuation ))
5420
21+ remaining_length = length - len (required_chars )
5522
56- Password = generatePassword (passwordLengths )
57-
58- for i in range (numPasswords ):
59- print ("Password #" + str (i + 1 )+ " = " + Password [i ])
60-
23+ password = required_chars + [secrets .choice (pool ) for _ in range (remaining_length )]
24+ secrets .SystemRandom ().shuffle (password )
25+
26+ return '' .join (password )
27+
28+ def save (passwords : list ) -> None :
29+ try :
30+ agree : str = input ("Do you want to save the passwords to a file? (y/n): " ).lower ().strip ()
31+ if agree not in ["y" , "yes" ]:
32+ return
33+ else :
34+ fileName : str = input ("Enter the name of the file (without .txt): " ).strip ()
35+ if not fileName :
36+ fileName : str = "passwords"
37+ print ("\n Saving passwords to file..." )
38+ with open (fileName + ".txt" , "w" ) as f :
39+ for password in passwords :
40+ f .write (password + "\n " )
41+ print ("Passwords saved successfully!" )
42+ except OSError as e :
43+ print (f"Error saving passwords: { e } " )
6144
45+ def main ():
46+ try :
47+ length : int = int (input ("Enter the length of the password: " ))
48+ count : int = int (input ("Enter the number of passwords to generate: " ))
49+
50+ if length < 1 or count < 1 :
51+ print ("Please enter positive integers only." )
52+ return
53+
54+ if length > 128 :
55+ print ("Password length cannot be greater than 128 characters." )
56+ return
57+
58+ if length < 6 :
59+ print ("Password is too short - Generating Passwords of length 6" )
60+ length = 6
61+
62+ digits : bool = input ("Include digits? (y/n): " ).lower ().strip () in ["y" , "yes" ]
63+ symbols : bool = input ("Include symbols? (y/n): " ).lower ().strip () in ["y" , "yes" ]
64+
65+ print ("\n Generating Secure Passwords...\n " )
66+ passwords = []
67+ for i in range (count ):
68+ password = generate_password (length , digits = digits , symbols = symbols )
69+ passwords .append (password )
70+ print (f"Password #{ i + 1 } : { password } " )
71+
72+ save (passwords )
73+
74+ except ValueError :
75+ print ("Please Enter Valid Integers Only" )
76+ except Exception as e :
77+ print (f"An unexpected error occurred: { e } " )
6278
63- main ()
79+ if __name__ == "__main__" :
80+ main ()
0 commit comments