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