-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
92 lines (73 loc) · 2.89 KB
/
main.py
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
import string
import secrets
import random
def generate_password(length=12, include_digits=True, include_symbols=True, exclude_ambiguous=True):
"""Generates a random password based on given parameters.
Args:
length (int, optional): Password length. Defaults to 12.
include_digits (bool, optional): Include digits. Defaults to True.
include_symbols (bool, optional): Include symbols. Defaults to True.
exclude_ambiguous (bool, optional): Exclude ambiguous characters. Defaults to True.
Returns:
str: Generated password.
"""
if length <= 0:
raise ValueError("Password length must be positive.")
characters = string.ascii_letters
if include_digits:
characters += string.digits
if include_symbols:
characters += string.punctuation
if exclude_ambiguous:
ambiguous_chars = 'l1IiL0Oo'
characters = ''.join(c for c in characters if c not in ambiguous_chars)
return ''.join(secrets.choice(characters) for _ in range(length))
def check_password_strength(password):
"""Checks password strength based on basic criteria.
Args:
password (str): The password to check.
Returns:
str: Strength level (weak, medium, strong).
"""
lower = any(c.islower() for c in password)
upper = any(c.isupper() for c in password)
digit = any(c.isdigit() for c in password)
special = any(not c.isalnum() for c in password)
if sum([lower, upper, digit, special]) < 3:
return "weak"
elif sum([lower, upper, digit, special]) < 4:
return "medium"
else:
return "strong"
def get_yes_no_input(prompt, default_value="y"):
"""Gets yes/no input from the user with error handling.
Args:
prompt (str): The prompt to display to the user.
default_value (str, optional): The default value to assume if no input is provided. Defaults to "y".
Returns:
bool: True if the user enters "y" (or the default value), False otherwise.
"""
while True:
user_input = input(prompt + " ") or default_value
if user_input.lower() in ("y", "n"):
return user_input.lower() == "y"
else:
print("Error: Invalid input. Please enter 'y' or 'n'.")
def main():
try:
length = int(input("Enter password length (default 12): ") or 12)
include_digits = get_yes_no_input("Include digits? (y/n, default y): ")
include_symbols = get_yes_no_input("Include symbols? (y/n, default y): ")
exclude_ambiguous = get_yes_no_input("Exclude ambiguous chars? (y/n, default y): ")
password = generate_password(
length=length,
include_digits=include_digits,
include_symbols=include_symbols,
exclude_ambiguous=exclude_ambiguous,
)
print("Generated Password:", password)
print("Password Strength:", check_password_strength(password))
except ValueError as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()