-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpasswd_gen.py
More file actions
104 lines (82 loc) · 3.53 KB
/
Copy pathpasswd_gen.py
File metadata and controls
104 lines (82 loc) · 3.53 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
# Script: password_generator.py
# Description: Tool that generates Strong and Random Passwords for Users.
# Author: Mohammed Abdul Raqeeb
# Date: 14/06/2024
try:
from colorama import Fore, Style
except ImportError:
print("This script requires the 'colorama' module.\nPlease install it using 'pip install colorama' and try again.")
exit(1)
import string
from random import shuffle
from secrets import choice
from time import sleep
# Define character sets
LOWER_CHARS = string.ascii_lowercase
UPPER_CHARS = string.ascii_uppercase
DIGITS = string.digits
SPECIAL_CHARS = string.punctuation
# Define colorama attributes
cyan = Fore.CYAN
green = Fore.GREEN
blue = Fore.BLUE
red = Fore.RED
yellow = Fore.YELLOW
bright = Style.BRIGHT
reset = Style.RESET_ALL
# Function to generate password
def generate_password(password_length, use_lower=False, use_upper=False, use_digits=False, use_special=False):
"""
Generates a strong random password of a specified length based on user preferences.
Parameters:
password_length (int): The length of the password to be generated.
use_lower (bool): Include lowercase letters if True.
use_upper (bool): Include uppercase letters if True.
use_digits (bool): Include digits if True.
use_special (bool): Include special characters if True.
Returns:
str: The generated password.
"""
char_pool = ''
password = []
if use_lower:
char_pool += LOWER_CHARS
password.append(choice(LOWER_CHARS))
if use_upper:
char_pool += UPPER_CHARS
password.append(choice(UPPER_CHARS))
if use_digits:
char_pool += DIGITS
password.append(choice(DIGITS))
if use_special:
char_pool += SPECIAL_CHARS
password.append(choice(SPECIAL_CHARS))
if not char_pool:
print(f"{red}\nNo character types selected for password generation.{reset}")
exit(1)
# Add random characters to fill the rest of the password length
password += [choice(char_pool) for _ in range(password_length - len(password))]
# Shuffle the password to ensure randomness
for _ in range(3):
shuffle(password)
return ''.join(password)
def main():
print(f"\n{yellow}{bright}" + " PASSWORD GENERATOR ".center(40, "-") + f"{reset}")
sleep(0.75)
try:
password_length = int(input(f'\n{cyan}Enter the length of the password to be generated: {reset}').strip())
if password_length < 4 or password_length > 256:
print(f"\n{red}Password length must be between 4 and 256 characters.\nPlease try again.{reset}\n")
exit(1)
print(f"{yellow}\n\nSelect character types to include in the password:")
use_lower = input(f'{green}\nInclude lowercase letters (y/n) {blue}[default: y]: {reset}').strip().lower() in ['','y','yes']
use_upper = input(f'{green}Include uppercase letters (y/n) {blue}[default: y]: {reset}').strip().lower() in ['','y','yes']
use_digits = input(f'{green}Include digits (y/n) {blue}[default: y]: {reset}').strip().lower() in ['','y','yes']
use_special = input(f'{green}Include special characters (y/n) {blue}[default: y]: {reset}').strip().lower() in ['','y','yes']
password = generate_password(password_length, use_lower, use_upper, use_digits, use_special)
print(f'\n\n{green}Your Robust Generated Password is: \n{yellow}{bright}{password}{reset}\n')
except ValueError:
print(f"\n{red}Invalid input.\nPlease enter a valid integer for the password length.{reset}\n")
exit(1)
if __name__ == '__main__':
main()