Created
August 6, 2025 04:44
-
-
Save lordsutch/a34b2995632740e073ad71372c2e456f to your computer and use it in GitHub Desktop.
Python 3.6+ code to automate the generation of Diceware passwords
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import argparse | |
| import re | |
| import secrets | |
| import sys | |
| WORDS = 7 | |
| # Can also use e.g. https://www.eff.org/deeplinks/2016/07/new-wordlists-random-passphrases | |
| WORDLIST = 'diceware.wordlist.asc' | |
| parser = argparse.ArgumentParser(description="create Diceware passwords") | |
| parser.add_argument('-w', '--length', '--words', type=int, | |
| default=WORDS, | |
| help=f'number of words in passphrase (default: {WORDS})') | |
| parser.add_argument('-i', '--wordlist', '--input', type=str, | |
| default=WORDLIST, | |
| help=f'wordlist to use (default: {WORDLIST})') | |
| args = parser.parse_args() | |
| dicelist = {} | |
| dicewords = open(args.wordlist, 'r') | |
| matchre = re.compile(r'^([1-6]+)\s+(.*)$') | |
| rolls = 5 | |
| for line in dicewords: | |
| match = matchre.match(line) | |
| if match: | |
| dicelist[match.group(1)] = match.group(2) | |
| rolls = len(match.group(1)) | |
| wordlist = [] | |
| passphrase = [] | |
| secrand = secrets.SystemRandom() | |
| for word in range(args.length): | |
| wordstr = ''.join(secrand.choices('123456', k=rolls)) | |
| wordlist.append(wordstr) | |
| passphrase.append(dicelist[wordstr]) | |
| print(wordlist) | |
| print('"'+' '.join(passphrase)+'"') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment