2025-12-06

Build a Python Password Generator Module in Under 50 Lines

Python, Security · Dorian Sotpyrc

Before we get into it: browser or code?

You can approach this password generator in two ways:

This article is about how that tiny engine works: a single-file Python module that can generate both strong random passwords and readable passphrases in under 50 lines.

A PLEX-styled illustration of a secure password generator with a glowing lock and cosmic gradients
A compact Python engine behind a clean, PLEX-styled password generator.

Why bother writing your own password generator?

There are a million password generators on the internet. Most of them have three problems:

  • They hide how randomness works, so you cannot audit or extend them.
  • They mix UI, logic, and storage into one opaque blob.
  • They rarely ship as a clean, importable Python module for scripts and tools.

Here the goal is different. You get:

  • a small, auditable module built on Python’s secrets library,
  • a CLI entrypoint you can run on any server or laptop, and
  • a browser tool that sits on top without changing the core logic.
Security first, aesthetics second

The minimal implementation is deliberate. Fewer branches and dependencies make it easier to review, test, and reuse the generator in scripts, cron jobs, or other PLEX tools.

The under-50-line core

The module exposes two functions: generate_strong_password for cryptographic passwords and generate_memorable_password for readable passphrases. Both sit in a single file so you can scan the entire logic in one go.

PYTHON
from password_gen import (
    generate_strong_password,
    generate_memorable_password,
)

# Strong 24-character password with special characters
print(generate_strong_password(length=24, use_special=True))

# 4-word memorable passphrase, capitalised, with a trailing number
print(
    generate_memorable_password(
        words=4,
        separator="-",
        caps=True,
        add_number=True,
    )
)

Under the hood everything runs through secrets.choice over carefully chosen character sets or word lists. There is no global state, no I/O, and nothing that depends on a framework. That makes the module ideal for both command-line usage and web tools.

Simple mode vs advanced mode

On the PLEX tool page you see two layers of UX for the same engine:

  • Simple mode – a length slider between 10 and 24 characters that always generates a strong password with mixed character types and special symbols.
  • Advanced mode – a toggle that reveals:
    • strong vs memorable mode,
    • custom length for strong passwords, and
    • word count, separator, capitalisation, and trailing number for passphrases.

The form elements are just parameter controls. The actual security behaviour lives entirely inside the module, not inside the HTML or JavaScript. That is the separation you want.

Design pattern: thin UI, fat core

Treat the web UI as a remote control. The real rules about length, character sets, and entropy belong in the Python module, where you can test and version them.

How to drop this into your own projects

Once the module is on your machine, you can use it far beyond the PLEX tool:

  • Wire it into a small script that prints passwords for new database users and logs only a one-way hash.
  • Add it to a deployment helper that generates API keys or shared secrets on the fly.
  • Use the memorable mode when onboarding non-technical users who struggle with random strings.

The important part is that you do not reinvent the wheel in ten different places. Keep one clean generator module and reuse it everywhere that needs new credentials.

Grab the code and tool

Clone the repo: python-password-generator , or try it in the browser via the PLEX Password Generator tool. Both are designed to be inspected, forked, and dropped into your own projects.

Security reminders you should not skip

  • Store passwords in a manager, not in plain text files or chat messages.
  • Never reuse the same password across different services.
  • Pair strong passwords with multi-factor authentication wherever possible.
  • Rotate credentials if you suspect a compromise, even if the generator is solid.

Related PLEX reading

References & further reading