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.py
More file actions
80 lines (62 loc) · 2.67 KB
/
Copy pathpassword.py
File metadata and controls
80 lines (62 loc) · 2.67 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
import secrets
import string
def generate_password(length: int = 12, digits: bool = True, symbols: bool = True) -> str:
min_required = int(digits) + int(symbols)
if length < min_required:
raise ValueError("Password length too small for selected options")
pool = string.ascii_letters
required_chars = []
if digits:
pool += string.digits
required_chars.append(secrets.choice(string.digits))
if symbols:
pool += string.punctuation
required_chars.append(secrets.choice(string.punctuation))
remaining_length = length - len(required_chars)
password = required_chars + [secrets.choice(pool) for _ in range(remaining_length)]
secrets.SystemRandom().shuffle(password)
return ''.join(password)
def save(passwords: list) -> None:
try:
agree: str = input("Do you want to save the passwords to a file? (y/n): ").lower().strip()
if agree not in ["y", "yes"]:
return
else:
fileName: str = input("Enter the name of the file (without .txt): ").strip()
if not fileName:
fileName: str = "passwords"
print("\nSaving passwords to file...")
with open(fileName + ".txt", "w") as f:
for password in passwords:
f.write(password + "\n")
print("Passwords saved successfully!")
except OSError as e:
print(f"Error saving passwords: {e}")
def main() -> None:
try:
length: int = int(input("Enter the length of the password: "))
count: int = int(input("Enter the number of passwords to generate: "))
if length < 1 or count < 1:
print("Please enter positive integers only.")
return
if length > 128:
print("Password length cannot be greater than 128 characters.")
return
if length < 6:
print("Password is too short - Generating Passwords of length 6")
length = 6
digits: bool = input("Include digits? (y/n): ").lower().strip() in ["y", "yes"]
symbols: bool = input("Include symbols? (y/n): ").lower().strip() in ["y", "yes"]
print("\nGenerating Secure Passwords...\n")
passwords = []
for i in range(count):
password = generate_password(length, digits=digits, symbols=symbols)
passwords.append(password)
print(f"Password #{i + 1}: {password}")
save(passwords)
except ValueError:
print("Please Enter Valid Integers Only")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
main()