Skip to content

Instantly share code, notes, and snippets.

@0x3333
Last active June 19, 2025 18:38
Show Gist options
  • Save 0x3333/debef99856d748c64f78f8b7ea4dc20c to your computer and use it in GitHub Desktop.
Save 0x3333/debef99856d748c64f78f8b7ea4dc20c to your computer and use it in GitHub Desktop.
Python Password generator

Password Generator

This Python script generates random passwords using various character sets and configurations. It provides flexibility in choosing password length and the number of passwords to generate. Generate passwords Microsoft 365 style(Cvcddddd).

Features

  • Generate passwords with different character sets:
    • Digits only
    • Lowercase letters and digits
    • Uppercase letters, lowercase letters, and digits
    • All printable characters (excluding control characters)
  • Generate passwords in CVC (Consonant-Vowel-Consonant) format followed by 5 digits
  • Customizable password length and count
  • Secure random number generation using the secrets module
  • Prints a header with single-digit column numbers (1-9), using a space for every 10th column

Usage

Run the script from the command line:

python password_generator.py [LENGTH] [COUNT]
  • length: Length of each password (default: 32, positive integer)
  • count: Number of passwords to generate (default: 20, positive integer)

Examples

Example 1: Generate passwords with default settings

./password_generator

Sample Output:

Generating 20 passwords of length 32

123456789 123456789 123456789 12   123456789 123456789 123456789 12   123456789 123456789 123456789 12   123456789 123456789 123456789 12

36534975313522508512477582609714   nkfyc7yimrbe04uepk6aosz4e6d122il   UwqFquvWuTvlGX6GgxJkMbFzhqQYE8qy   %Zq0O+O0}gT<c3!^v9L(fbc?e_#X5 9*   Kur88139
53418343617639933145915175139835   82qtqin46cowa67b9gh3am6olfvo9un0   gsFFdqjRbK1kt2Jmm9M9mSVwA8eb6xhQ   jIItfc|=N?s<|iC71+S%.^T4,Id#z=]o   Wav45960
02348689072177571112782644846104   vahslhahir603oxuxhohx0m1w67ztf02   UHjTb9Y8oGGLFliXFYTtwgv2M4kHPpo0   Csk*C#]1T"BEd0 S/fx'p#Rmw#b"S_GR   Toj73544
91421726792983049063388367028556   cama1xrmvcu0jhj8jqdtc90nq7q0hymm   rpsYFQF0fovLZC771MfIZd46bK5Qnz4D   *k8}@NE;Q3(D4$h_u0|C0$'`Dt1g9K'P   Qir24256
...

Example 2: Generate 10 passwords of length 16

python password_generator.py --length 16 --count 10

Sample Output:

Generating 10 passwords of length 16

123456789 123456   123456789 123456   123456789 123456   123456789 123456

1547024292815642   3odm52e9hqwpaee8   i9EBlXKUrF0rKKIE   M+\Kgo&T%;cpn-t\   Fim44611
9645942854661753   u41cjco5twp0h2i4   jSRt1Uie4yCHBkZI   ;n/HIrdwx r?V6-Y   Cuc20962
0257528002529535   frvgovsedtlcdjq9   aFB2zd0Riu3BLgpk   yAF^I>A@8z (c]VN   Nob40954
7554235848885875   qojwhsbddlgt6phi   ebRZ60P6h0J6mLha   HMg<H 7iWWPV^i_j   Mut50999
...

Note: The actual passwords generated will be random and different each time the script is run.

Requirements

  • Python 3.6+

Note on Security

This script uses the secrets module for generating cryptographically secure random numbers, making it suitable for creating strong, unpredictable passwords.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import secrets
import string
class PasswordGenerator:
# Define character sets as class-level constants
CONSONANTS = "bcdfghjklmnpqrstvwxyz"
VOWELS = "aeiou"
DIGITS_CHARS = string.digits
LOWERCASE_CHARS = string.ascii_lowercase
UPPERCASE_CHARS = string.ascii_uppercase
PRINTABLE_CHARS = string.printable[:-5] # Excludes tabs, newlines, etc.
# Precomputed combined character sets
LOWER_DIGITS = LOWERCASE_CHARS + DIGITS_CHARS
ALL_LETTERS_DIGITS = LOWERCASE_CHARS + UPPERCASE_CHARS + DIGITS_CHARS
def _generate_single_password(self, length: int, charset: str) -> str:
"""Generates a single random password of a given length and character set."""
return "".join(secrets.choice(charset) for _ in range(length))
def generate_batch(self, length: int, count: int, charset: str) -> list[str]:
"""Generates a batch of random passwords of a given length and character set."""
return [self._generate_single_password(length, charset) for _ in range(count)]
def generate_cvc_digits_passwords(self, count: int) -> list[str]:
"""Generates a batch of passwords in CVC format followed by 5 digits."""
passwords = []
for _ in range(count):
first_consonant = secrets.choice(self.CONSONANTS).upper()
vowel = secrets.choice(self.VOWELS)
second_consonant = secrets.choice(self.CONSONANTS)
random_digits = self._generate_single_password(5, self.DIGITS_CHARS)
passwords.append(
f"{first_consonant}{vowel}{second_consonant}{random_digits}"
)
return passwords
def generate_header(self, length: int) -> str:
"""Creates a header string for formatting output."""
return "".join(
str(i % 10) if i % 10 != 0 else " " for i in range(1, length + 1)
)
def main():
parser = argparse.ArgumentParser(
description="Generate random passwords with various character sets."
)
parser.add_argument(
"length",
type=int,
default=32,
nargs="?",
help="Length of each password (default: 32)",
)
parser.add_argument(
"count",
type=int,
default=20,
nargs="?",
help="Number of passwords to generate (default: 20)",
)
args = parser.parse_args()
if args.length <= 0 or args.count <= 0:
parser.error("Length and count must be positive integers")
print(f"Generating {args.count} passwords of length {args.length}\n")
generator = PasswordGenerator()
charsets = [
generator.DIGITS_CHARS,
generator.LOWER_DIGITS,
generator.ALL_LETTERS_DIGITS,
generator.PRINTABLE_CHARS,
]
all_generated_passwords = []
for charset in charsets:
batch = generator.generate_batch(args.length, args.count, charset)
all_generated_passwords.append([generator.generate_header(args.length), *batch])
cvc_passwords = ["\n", *generator.generate_cvc_digits_passwords(args.count)]
all_generated_passwords.append(cvc_passwords)
for row in map(list, zip(*all_generated_passwords)):
template = f"{{: <{args.length + 3}}}" * len(charsets) + "{: >8}"
print(template.format(*row))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment