Repository navigation
Expand file tree
/
Copy pathpassword_generator.py
More file actions
74 lines (59 loc) · 2.25 KB
/
Copy pathpassword_generator.py
File metadata and controls
74 lines (59 loc) · 2.25 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
import random
import string
from typing import List
class PasswordGenerator:
@staticmethod
def generate_password(
length: int = 12,
include_uppercase: bool = True,
include_numbers: bool = True,
include_special: bool = True
) -> str:
"""Generate a random password with specified criteria"""
if length < 4:
length = 4
# Base character set (lowercase letters)
chars = string.ascii_lowercase
# Required characters to ensure at least one of each type
required_chars: List[str] = []
if include_uppercase:
chars += string.ascii_uppercase
required_chars.append(random.choice(string.ascii_uppercase))
if include_numbers:
chars += string.digits
required_chars.append(random.choice(string.digits))
if include_special:
special_chars = "!@#$%^&*()_+-=[]{}|;:,.<>?"
chars += special_chars
required_chars.append(random.choice(special_chars))
# Always include at least one lowercase letter
required_chars.append(random.choice(string.ascii_lowercase))
# Generate remaining characters
remaining_length = length - len(required_chars)
random_chars = [random.choice(chars) for _ in range(remaining_length)]
# Combine and shuffle
password_chars = required_chars + random_chars
random.shuffle(password_chars)
return ''.join(password_chars)
@staticmethod
def get_password_strength(password: str) -> str:
"""Evaluate password strength"""
score = 0
if len(password) >= 8:
score += 1
if len(password) >= 12:
score += 1
if any(c.islower() for c in password):
score += 1
if any(c.isupper() for c in password):
score += 1
if any(c.isdigit() for c in password):
score += 1
if any(c in "!@#$%^&*()_+-=[]{}|;:,.<>?" for c in password):
score += 1
if score <= 2:
return "Weak"
elif score <= 4:
return "Medium"
else:
return "Strong"