|
| 1 | +import random |
| 2 | +import string |
| 3 | + |
| 4 | + |
| 5 | +def main(): |
| 6 | + while True: |
| 7 | + option = input( |
| 8 | + "What would you like to do:\n" |
| 9 | + "1 Generate a secure password\n" |
| 10 | + "2 Check strength of my password\n> " |
| 11 | + ) |
| 12 | + if option not in ("1", "2"): |
| 13 | + print("Please choose 1 or 2.") |
| 14 | + continue |
| 15 | + break |
| 16 | + |
| 17 | + if option == "1": |
| 18 | + while True: |
| 19 | + try: |
| 20 | + choice = int( |
| 21 | + input( |
| 22 | + "Choose password type:\n" |
| 23 | + "1 Mix of numbers, letters and symbols (Recommended)\n" |
| 24 | + "2 Numbers only password\n" |
| 25 | + "3 Letters only password\n" |
| 26 | + "4 Symbols only password\n> " |
| 27 | + ) |
| 28 | + ) |
| 29 | + length = int(input("Enter your desired length (between 4 and 20): ")) |
| 30 | + except ValueError: |
| 31 | + print("Invalid input, enter numbers only.") |
| 32 | + continue |
| 33 | + |
| 34 | + if choice not in (1, 2, 3, 4) or length not in range(4, 21): |
| 35 | + print("Invalid input, try again.") |
| 36 | + continue |
| 37 | + break |
| 38 | + |
| 39 | + if choice == 1: |
| 40 | + passwd = mix_of_all(length) |
| 41 | + elif choice == 2: |
| 42 | + passwd = generate_number_only(length) |
| 43 | + elif choice == 3: |
| 44 | + passwd = generate_letters_only(length) |
| 45 | + else: |
| 46 | + passwd = generate_symbols_only(length) |
| 47 | + |
| 48 | + print("Here is your password:", passwd) |
| 49 | + |
| 50 | + if input("Would you like a report for this password? (y/n): ").lower() == "y": |
| 51 | + print(password_report(passwd)) |
| 52 | + |
| 53 | + else: # option == "2" |
| 54 | + existing = input("Enter the password you want to check: ") |
| 55 | + print(password_report(existing)) |
| 56 | + |
| 57 | + |
| 58 | +def generate_number_only(length): |
| 59 | + digits = string.digits |
| 60 | + return "".join(random.choice(digits) for _ in range(length)) |
| 61 | + |
| 62 | + |
| 63 | +def generate_letters_only(length): |
| 64 | + letters = string.ascii_letters |
| 65 | + return "".join(random.choice(letters) for _ in range(length)) |
| 66 | + |
| 67 | + |
| 68 | +def generate_symbols_only(length): |
| 69 | + symbols = "!@#$%^&*()-_=+[]{};:,.<>?/\\|" |
| 70 | + return "".join(random.choice(symbols) for _ in range(length)) |
| 71 | + |
| 72 | + |
| 73 | +def mix_of_all(length): |
| 74 | + pool = string.ascii_letters + string.digits + "!@#$%^&*()-_=+[]{};:,.<>?/\\|" |
| 75 | + return "".join(random.choice(pool) for _ in range(length)) |
| 76 | + |
| 77 | + |
| 78 | +def password_report(password: str) -> str: |
| 79 | + recommended_length = 8 |
| 80 | + |
| 81 | + length = len(password) |
| 82 | + upper = sum(1 for ch in password if ch.isupper()) |
| 83 | + lower = sum(1 for ch in password if ch.islower()) |
| 84 | + digits = sum(1 for ch in password if ch.isdigit()) |
| 85 | + symbols = sum(1 for ch in password if not ch.isalnum()) |
| 86 | + |
| 87 | + parts = [] |
| 88 | + |
| 89 | + # Length report |
| 90 | + diff = recommended_length - length |
| 91 | + if diff > 0: |
| 92 | + parts.append( |
| 93 | + f"The password has a length of {length} characters, {diff} less than the recommended {recommended_length}." |
| 94 | + ) |
| 95 | + else: |
| 96 | + parts.append( |
| 97 | + f"The password has a length of {length} characters, which meets or exceeds the recommended {recommended_length}." |
| 98 | + ) |
| 99 | + |
| 100 | + # Composition report |
| 101 | + parts.append( |
| 102 | + f"It has {upper} uppercase letter(s), {lower} lowercase letter(s), {digits} number(s), and {symbols} symbol(s)." |
| 103 | + ) |
| 104 | + |
| 105 | + suggestions = [] |
| 106 | + if upper == 0: |
| 107 | + suggestions.append("add at least one uppercase letter") |
| 108 | + if lower == 0: |
| 109 | + suggestions.append("add at least one lowercase letter") |
| 110 | + if digits == 0: |
| 111 | + suggestions.append("add at least one number") |
| 112 | + if symbols == 0: |
| 113 | + suggestions.append("add a symbol for extra strength") |
| 114 | + |
| 115 | + if suggestions: |
| 116 | + parts.append( |
| 117 | + "To improve this password, you could " + ", ".join(suggestions) + "." |
| 118 | + ) |
| 119 | + else: |
| 120 | + parts.append("This password has a good mix of character types.") |
| 121 | + |
| 122 | + return " ".join(parts) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == '__main__': |
| 126 | + main() |
0 commit comments