-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_generator.py
121 lines (93 loc) · 4.11 KB
/
password_generator.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
Generate a strong password of desired length without repeating any characters.
Password requirements:
- Should contain at least one lowercase letter
- Should contain at least one uppercase letter
- Should contain at least one digit 0-9
- Should contain at least one special or user defined character !#$%&()*+,-.:;<=>?@[]^_`{|}~
Example:
>>> Password Length: 12
>>> Provide Special Characters (optional):
>>> A#v!10_kJzxL
>>> Password Length: 14
>>> Provide Special Characters (optional): &*#@()
>>> F4fEyR(m50DxJ#
.. module:: password_generator
:platform: Unix, Windows
:synopsis: This module provides functions to generate strong passwords.
.. moduleauthor:: Kshitij Satpute <[email protected]>
"""
import string
import random
def generate_password(
password_length: int, special_chars: str = "!#$%&()*+,-.:;<=>?@[]^_`{|}~"
) -> str:
"""
Generate a strong password of the given length without repeating any
characters.
:param password_length: The desired length of the password.
:param special_chars: User defined special characters.
:return: The generated password.
"""
if not special_chars:
special_chars = "".join(c for c in string.punctuation if c not in "\\/\"'")
password_characters: str = string.ascii_letters + string.digits + special_chars
password: str = "".join(random.sample(password_characters, password_length))
# Check if the generated password meets the strict password requirements.
while not __is_strict_password(password, special_chars):
password = generate_password(password_length)
return password
def __is_strict_password(password: str, special_chars: str) -> bool:
"""
Check if a given password meets the strict password requirements.
Password requirements:
- Should contain at least one lowercase letter
- Should contain at least one uppercase letter
- Should contain at least one digit `(0-9)`
- Should contain at least one special or user defined character !#$%&()*+,-.:;<=>?@[]^_`{|}~
:param password: The password to be checked.
:param special_chars: User defined special characters.
:return: True if the password meets the requirements, False otherwise.
:rtype: bool
"""
lowercase: bool = any(char.islower() for char in password)
uppercase: bool = any(char.isupper() for char in password)
digits: bool = any(char.isdigit() for char in password)
special_characters: bool = any(char in special_chars for char in password)
return lowercase and uppercase and digits and special_characters
def main() -> None:
"""
The main function that prompts the user for the desired password length
and optional user defined special characters.
Generates a strong password satisfying the strict password requirements.
Password requirements:
- Password length >=8, <=18
- Should contain at least one lowercase letter
- Should contain at least one uppercase letter
- Should contain at least one digit 0-9
- Should contain at least one special or user defined character !#$%&()*+,-.:;<=>?@[]^_`{|}~
Example:
>>> Password Length: 12
>>> Provide Special Characters (optional):
>>> A#v!10_kJzxL
>>> Password Length: 14
>>> Provide Special Characters (optional): &*#@()
>>> F4fEyR(m50DxJ#
"""
print("Password requirements:")
print("- Should contain at least one lowercase letter")
print("- Should contain at least one uppercase letter")
print("- Should contain at least one digit 0-9")
print(
"- Should contain at least one special character !#$%&()*+,-.:;<=>?@[]^_`{|}~"
)
pass_len: int = int(input("Password Length (8-18): "))
if pass_len < 8 or pass_len > 18:
print("Password length should be >=8 and <=18")
# Re-prompt the user for the password length.
pass_len = int(input("Password Length (8-18), Ctrl+C to exit: "))
special_chars = input("Provide Special Characters (optional): ")
password: str = generate_password(pass_len, special_chars)
print(password)
if __name__ == "__main__":
main()