Skip to content

Instantly share code, notes, and snippets.

@rjzak
Created December 30, 2016 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjzak/1619a7def053515f847ec98381fadaed to your computer and use it in GitHub Desktop.
Save rjzak/1619a7def053515f847ec98381fadaed to your computer and use it in GitHub Desktop.
A Python 3 script which uses the system's dictionaries to generate xkcd-inspired passwords. Inspired by: https://xkcd.com/936/
#!/usr/bin/python3
import os
import sys
import glob
import codecs
import random
cleanup = lambda x: x.split("/")[0] if "/" in x else x # Some hunspell entries have slashes in the second to last character.
num = 4
if len(sys.argv) > 1:
try:
num = int(sys.argv[1])
except:
pass
words = list()
dicts = list()
if os.path.exists("/usr/share/hunspell"):
dicts += glob.glob("/usr/share/hunspell/*.dic")
if os.path.exists("/usr/share/dict"):
dicts += glob.glob("/usr/share/dict/*-*")
for dictfile in dicts:
with open(dictfile, encoding="utf-8", errors="ignore") as f:
[words.append(cleanup(word.strip())) for word in f]
words = list(set(words))
password = ' '.join(random.choice(words) for i in range(num))
print(password)
print("Generated from %d dictionary files." % len(dicts))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment