-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword_generator.py
More file actions
90 lines (72 loc) · 2.65 KB
/
password_generator.py
File metadata and controls
90 lines (72 loc) · 2.65 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
import random
import string
def generate_password(length):
"""
Generates a secure password of specified length.
The password includes at least one uppercase letter,
one lowercase letter, one digit, and one special character.
Args:
length (int): The total length of the password (minimum 4).
Returns:
str: The generated password.
"""
if length < 4:
raise ValueError("Password length must be at least 4 characters.")
# Character categories
uppercase = random.choice(string.ascii_uppercase)
lowercase = random.choice(string.ascii_lowercase)
digit = random.choice(string.digits)
special = random.choice('!@#$%^&*()-_=+[]{}|;:,.<>?/')
# Combine all characters
all_chars = string.ascii_letters + string.digits + '!@#$%^&*()-_=+[]{}|;:,.<>?/'
# Generate random characters to fill the rest of the password length
remaining_length = length - 4
if remaining_length > 0:
remaining_chars = random.choices(all_chars, k=remaining_length)
else:
remaining_chars = []
# Combine all parts of the password
password_list = [uppercase, lowercase, digit, special] + remaining_chars
# Shuffle the list to ensure randomness
random.shuffle(password_list)
# Convert the list to a string to form the final password
password = ''.join(password_list)
return password
def calculate_strength(password):
"""
Calculates the strength of the password.
Args:
password (str): The password to evaluate.
Returns:
str: A string indicating the strength level.
"""
length = len(password)
categories = 0
if any(c.islower() for c in password):
categories += 1
if any(c.isupper() for c in password):
categories += 1
if any(c.isdigit() for c in password):
categories += 1
if any(c in '!@#$%^&*()-_=+[]{}|;:,.<>?/' for c in password):
categories += 1
# Simple strength evaluation logic
if length >= 12 and categories == 4:
return 'Very Strong'
elif length >= 8 and categories >= 3:
return 'Strong'
elif length >= 6 and categories >= 2:
return 'Medium'
else:
return 'Weak'
if __name__ == "__main__":
print("Welcome to the Secure Password Generator!")
try:
length_input = input("Enter the desired password length (minimum 4): ")
length = int(length_input)
password = generate_password(length)
strength = calculate_strength(password)
print(f"\nGenerated Password: {password}")
print(f"Password Strength: {strength}")
except ValueError as ve:
print(f"Error: {ve}")