πŸ› οΈToolsShed

Secure Passphrase Generator

Generate memorable Diceware-style passphrases with entropy estimation.

38

Frequently Asked Questions

Code Implementation

import secrets
import math

# A small built-in word list; replace with a full EFF large wordlist in production.
WORDLIST = [
    "apple", "brave", "crane", "drift", "eagle", "flame", "grace", "hover",
    "ivory", "jewel", "knack", "lunar", "maple", "noble", "ocean", "pearl",
    "quilt", "raven", "stone", "tiger", "umbra", "vivid", "waltz", "xenon",
    "yacht", "zonal", "amber", "blaze", "crisp", "delta", "ember", "frost",
    "globe", "haste", "index", "joust", "karma", "lance", "marsh", "nerve",
    "ozone", "pilot", "query", "ridge", "storm", "trove", "ultra", "vigor",
    "width", "xylem", "yearn", "zesty",
]

def generate_passphrase(
    word_count: int = 4,
    separator: str = "-",
    capitalize: bool = False,
    append_number: bool = False,
) -> str:
    words = [secrets.choice(WORDLIST) for _ in range(word_count)]
    if capitalize:
        words = [w.capitalize() for w in words]
    phrase = separator.join(words)
    if append_number:
        phrase += separator + str(secrets.randbelow(9000) + 1000)
    return phrase

def passphrase_entropy(word_count: int, wordlist_size: int) -> float:
    """Bits of entropy: log2(wordlist_size ^ word_count)"""
    return word_count * math.log2(wordlist_size)


if __name__ == "__main__":
    for _ in range(5):
        phrase = generate_passphrase(4, "-", capitalize=True, append_number=True)
        print(phrase)
    bits = passphrase_entropy(4, len(WORDLIST))
    print(f"Entropy with {len(WORDLIST)}-word list, 4 words: {bits:.1f} bits")

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.