|
1 | 1 | import string
|
2 | 2 | import secrets
|
3 | 3 |
|
4 |
| -# Define the set of characters to be used in the password |
5 |
| -CHARACTER_SET = string.ascii_letters + string.digits + string.punctuation |
| 4 | +from enum import Enum |
6 | 5 |
|
7 |
| -def generate_password(length): |
8 |
| - """Generate a random password of the specified length.""" |
9 |
| - password = ''.join(secrets.choice(CHARACTER_SET) for i in range(length)) |
| 6 | + |
| 7 | +# Define the set of characters to be used in the password, three strings means three modes. |
| 8 | + |
| 9 | + |
| 10 | +class CharcterSet(Enum): |
| 11 | + ONLYNUMBER = string.digits |
| 12 | + NOPUNCTUATION = string.ascii_letters + string.digits |
| 13 | + ALLCHARCTER = string.ascii_letters + string.digits + string.punctuation |
| 14 | + |
| 15 | + |
| 16 | +# Define a list of CharcterSet , in order to quickly transfer mode code to corresponding character set. |
| 17 | + |
| 18 | +CharcterSetList = [ |
| 19 | + CharcterSet.ALLCHARCTER, |
| 20 | + CharcterSet.ONLYNUMBER, |
| 21 | + CharcterSet.NOPUNCTUATION, |
| 22 | +] |
| 23 | + |
| 24 | +# Define Prompt Enum class , in order to amend prompts more easily . |
| 25 | + |
| 26 | + |
| 27 | +class Prompt(Enum): |
| 28 | + GetPasswordInt = "How many passwords do you want to generate? " |
| 29 | + GetPasswordLength = "Enter the length of the password(s): " |
| 30 | + GetGenerationMode = "Choose password generation mode: (1) Only Number (2) Don't use punctuation (0) All ascii character " |
| 31 | + ValueErrorOccured = "Please enter a valid integer." |
| 32 | + PasswordGenerated = "Generated passwords:" |
| 33 | + |
| 34 | + |
| 35 | +def generate_password(length: int, generation_mode_code: int = 0) -> str: |
| 36 | + """ |
| 37 | + generate_password |
| 38 | +
|
| 39 | + Generate a random password of the specified length by the specified mode. |
| 40 | +
|
| 41 | + Args: |
| 42 | + length (int): specify the length of password. |
| 43 | + generation_mode_code (int): specify the generation mode of password: (1) Only Number (2) Don't use punctuation (0/default) All ascii character. |
| 44 | +
|
| 45 | + Returns: |
| 46 | + str: a password string |
| 47 | + """ |
| 48 | + password = "".join( |
| 49 | + secrets.choice(CharcterSetList[generation_mode_code].value) |
| 50 | + for i in range(length) |
| 51 | + ) |
10 | 52 | return password
|
11 | 53 |
|
| 54 | + |
12 | 55 | def main():
|
13 | 56 | # Prompt the user for the number of passwords to generate and their length
|
14 | 57 | while True:
|
15 | 58 | try:
|
16 |
| - num_pass = int(input("How many passwords do you want to generate? ")) |
17 |
| - password_length = int(input("Enter the length of the password(s): ")) |
| 59 | + num_pass = int(input(Prompt.GetPasswordInt.value)) |
| 60 | + password_length = int(input(Prompt.GetPasswordLength.value)) |
| 61 | + generation_mode_code = int(input(Prompt.GetGenerationMode.value)) |
18 | 62 | break
|
19 | 63 | except ValueError:
|
20 |
| - print("Please enter a valid integer.") |
21 |
| - continue |
| 64 | + print(Prompt.ValueErrorOccured.value) |
| 65 | + continue |
22 | 66 | # Generate the specified number of passwords and print them to the console
|
23 |
| - print("Generated passwords:") |
| 67 | + print(Prompt.PasswordGenerated.value) |
24 | 68 | for i in range(num_pass):
|
25 |
| - password = generate_password(password_length) |
| 69 | + password = generate_password(password_length, generation_mode_code) |
26 | 70 | print(f"{i+1}. {password}")
|
27 | 71 |
|
| 72 | + |
28 | 73 | if __name__ == "__main__":
|
29 | 74 | main()
|
0 commit comments