Python Password Generator Script

diGi
3 min readOct 10, 2024

--

In September 2024, The National Institute of Standards and Technology (NIST), the federal body that sets the standards for government agencies, proposed new methods for passwords. Instead of making them complex, mixed with alphanumeric and special symbols, the length is the most important aspect of a password. They recommend to make your password a minimum of 15 characters in length and maximum of 64.

I created a Python password generator script that allows you to easily create secure, random passwords with customizable options such as length, number of passwords, and the inclusion of special characters. Whether you’re looking to generate strong passwords for yourself or for a project, this script provides flexibility and ease of use.

Script Features:

Default Behavior:
— By default, the script generates 15 passwords of length 24, consisting only of letters and numbers (no special characters).
— To run the script with default settings, use the following command:

$ python3 password_generator.py

Customizable Options:
You can customize the password generation by passing arguments to the script:
--length: Specifies the length of each password (default: 24).
--num_passwords: Specifies the number of passwords to generate (default: 15).
--special: If this flag is included, special characters will be added to the passwords. Special characters make up 2 to 5 characters of each password if enabled.

Example of customizing the password generation:

$ python3 password_generator.py --length 20 --num_passwords 10 --special

In this example:
10 passwords of length 20 will be generated, and special characters will be included.

Code:

import random
import argparse

# Define character sets
LETTERS_NUMBERS = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789'
SPECIAL_CHARS = '!#$%^+:<>/\=?@{}^~'

def generate_password(length, special=False):
password = []

if special:
# Determine how many special characters to include (min 2, max 5 or total length)
num_special_chars = min(max(2, random.randint(2, 5)), length)
num_letters_numbers = length - num_special_chars

# Step 1: Add special characters
for _ in range(num_special_chars):
password.append(random.choice(SPECIAL_CHARS))

# Step 2: Add letters and numbers
for _ in range(num_letters_numbers):
password.append(random.choice(LETTERS_NUMBERS))
else:
# No special characters, just letters and numbers
for _ in range(length):
password.append(random.choice(LETTERS_NUMBERS))

# Shuffle the password to mix the characters and return as a string
random.shuffle(password)
return ''.join(password)

def main():
# Default values
default_length = 24
default_num_passwords = 15
default_special = False

# Instructions for the user
print(f"Password Generator Script")
print(f"-------------------------")
print(f"By default, it will generate {default_num_passwords} passwords of length {default_length} without special characters.")
print(f"To customize your results, use the following arguments:")
print(f"--length: The length of each password (default: {default_length})")
print(f"--num_passwords: The number of passwords to generate (default: {default_num_passwords})")
print(f"--special: Include special characters (use this flag to enable special characters)")
print(f"Example: python3 password_generator.py --length 20 --num_passwords 10 --special")
print(f"-------------------------\n")

# Argument parser for command-line arguments
parser = argparse.ArgumentParser(description='Generate secure passwords.')
parser.add_argument('--length', type=int, default=default_length, help=f'The length of each password (default: {default_length}).')
parser.add_argument('--num_passwords', type=int, default=default_num_passwords, help=f'The number of passwords to generate (default: {default_num_passwords}).')
parser.add_argument('--special', action='store_true', help='Include special characters (default: False).')

args = parser.parse_args()

# Generate and print the passwords
for _ in range(args.num_passwords):
print(generate_password(args.length, args.special))

if __name__ == '__main__':
main()

This Python password generator is a simple tool to create secure passwords tailored to your specific needs. You can adjust the length, number of passwords, and whether to include special characters, making it flexible for various security needs.

--

--

diGi
diGi

Written by diGi

20+ years experience as a PHP web developer turned Cybersecurity professional. https://tryhackme.com/r/p/diGiJeff

No responses yet