import secrets
import string
def generate_password(length=16, use_upper=True, use_lower=True, use_digits=True, use_symbols=True):
if not (use_upper or use_lower or use_digits or use_symbols):
raise ValueError("At least one character set must be enabled!")
char_pool = ''
if use_upper:
char_pool += string.ascii_uppercase
if use_lower:
char_pool += string.ascii_lowercase
if use_digits:
char_pool += string.digits
if use_symbols:
char_pool += string.punctuation
# Guarantee at least one of each chosen type
password = []
if use_upper:
password.append(secrets.choice(string.ascii_uppercase))
if use_lower:
password.append(secrets.choice(string.ascii_lowercase))
if use_digits:
password.append(secrets.choice(string.digits))
if use_symbols:
password.append(secrets.choice(string.punctuation))
# Fill the rest of the password length
while len(password) < length:
password.append(secrets.choice(char_pool))
# Shuffle to avoid predictable order of char types
secrets.SystemRandom().shuffle(password)
return ''.join(password[:length])
def generate_multiple_passwords(n=5, **kwargs):
return [generate_password(**kwargs) for _ in range(n)]
# Example usage
if __name__ == "__main__":
num_passwords = int(input("How many passwords? "))
pwd_length = int(input("Password length? "))
print("Include uppercase letters? (y/n): ")
use_upper = input().strip().lower() == 'y'
print("Include lowercase letters? (y/n): ")
use_lower = input().strip().lower() == 'y'
print("Include digits? (y/n): ")
use_digits = input().strip().lower() == 'y'
print("Include symbols? (y/n): ")
use_symbols = input().strip().lower() == 'y'
passwords = generate_multiple_passwords(
n=num_passwords,
length=pwd_length,
use_upper=use_upper,
use_lower=use_lower,
use_digits=use_digits,
use_symbols=use_symbols
)
for i, pwd in enumerate(passwords, 1):
print(f"Password {i}: {pwd}")