-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword.py
More file actions
107 lines (91 loc) · 3.56 KB
/
password.py
File metadata and controls
107 lines (91 loc) · 3.56 KB
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
import string
from random import shuffle, randint, choice
class PasswordGenerator:
"""
Generates random passwords based on specified criteria.
"""
def _init_(self):
self.minlen = 6
self.maxlen = 16
self.minuchars = 1
self.minlchars = 1
self.minnumbers = 1
self.minschars = 1
self.excludeuchars = ""
self.excludelchars = ""
self.excludenumbers = ""
self.excludeschars = ""
self.lower_chars = string.ascii_lowercase
self.upper_chars = string.ascii_uppercase
self.numbers_list = string.digits
self._schars = "!#$%^&*(),.-_+="
def generate(self):
"""
Generates a password using default or custom properties.
"""
if any(
value < 0
for value in [
self.minlen,
self.maxlen,
self.minuchars,
self.minlchars,
self.minnumbers,
self.minschars,
]
):
raise ValueError("Character length should not be negative")
if self.minlen > self.maxlen:
raise ValueError(
"Minimum length cannot be greater than maximum length. The default maximum length is 16."
)
collective_min_length = (
self.minuchars + self.minlchars + self.minnumbers + self.minschars
)
if collective_min_length > self.minlen:
self.minlen = collective_min_length
final_pass = [
choice(list(set(self.lower_chars) - set(self.excludelchars)))
for _ in range(self.minlchars)
]
final_pass += [
choice(list(set(self.upper_chars) - set(self.excludeuchars)))
for _ in range(self.minuchars)
]
final_pass += [
choice(list(set(self.numbers_list) - set(self.excludenumbers)))
for _ in range(self.minnumbers)
]
final_pass += [
choice(list(set(self._schars) - set(self.excludeschars)))
for _ in range(self.minschars)
]
current_pass_len = len(final_pass)
all_chars = (
set(self.lower_chars)
| set(self.upper_chars)
| set(self.numbers_list)
| set(self._schars)
) - set(
list(self.excludelchars)
+ list(self.excludeuchars)
+ list(self.excludenumbers)
+ list(self.excludeschars)
)
if current_pass_len < self.maxlen:
rand_len = randint(self.minlen, self.maxlen)
final_pass += [choice(list(all_chars)) for _ in range(rand_len - current_pass_len)]
shuffle(final_pass)
return "".join(final_pass)
if __name__ == "_main_":
generator = PasswordGenerator()
# Prompt user for password criteria
generator.minlen = int(input("Enter the minimum length of the password: "))
generator.maxlen = int(input("Enter the maximum length of the password: "))
generator.minuchars = int(input("Enter the minimum number of uppercase characters: "))
generator.minlchars = int(input("Enter the minimum number of lowercase characters: "))
generator.minnumbers = int(input("Enter the minimum number of numeric characters: "))
generator.minschars = int(input("Enter the minimum number of special characters: "))
# Generate password based on the provided criteria
password = generator.generate()
print("Generated Password:",password)