Skip to content

Instantly share code, notes, and snippets.

@RWJMurphy
Last active December 12, 2015 03:08
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 RWJMurphy/4704588 to your computer and use it in GitHub Desktop.
Save RWJMurphy/4704588 to your computer and use it in GitHub Desktop.
Does what it says on the box. Inspired by http://xkcd.com/936/
#!/usr/bin/env python
from __future__ import print_function
import argparse
import math
import random
import string
import sys
class PassGen:
def __init__(self, dictionary, connectives, minimum_word_length, no_white_space=False):
self.dictionary_file = open(dictionary, "r")
self.connective_dictionary_file = open(connectives, "r")
self.minimum_word_length = minimum_word_length
self.join_character = '_' if no_white_space else ' '
self.words = [w.rstrip() for w in self.dictionary_file if len(w) >= self.minimum_word_length]
self.joins = [w.rstrip() for w in self.connective_dictionary_file]
self.symbols = string.punctuation
self.digits = string.digits
def generate(self, count=1, length=2):
return [self.__compose_phrase(length) for x in range(count)]
def entropy(self, length):
return math.log((len(self.joins) * len(self.words)) ** length. 2)
def __compose_phrase(self, length):
# word symbol word number
return self.join_character.join([
self.join_character.join((
random.choice(self.joins),
random.choice(self.words)
))
for x in range(length)
])
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--dictionary', '-d', default="/usr/share/dict/words")
parser.add_argument('--connectives', '-D', default="/usr/share/dict/connectives")
parser.add_argument('--count', '-c', default=1, type=int)
parser.add_argument('--minimum-word-length', '-m', default=10, type=int)
parser.add_argument('--no-white-space', '-W', action="store_true")
parser.add_argument('--length', '-l', type=int, default=2)
args = parser.parse_args()
pg = PassGen(args.dictionary, args.connectives, args.minimum_word_length, args.no_white_space)
print("Generating %d passwords with ~%d bits of entropy" % (args.count, pg.entropy(args.length)), file=sys.stderr)
print("\n".join(pg.generate(int(args.count), args.length)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment