Skip to content

Instantly share code, notes, and snippets.

@benhosmer
Created September 6, 2014 12:34
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 benhosmer/f52c2b3c2c93b4456633 to your computer and use it in GitHub Desktop.
Save benhosmer/f52c2b3c2c93b4456633 to your computer and use it in GitHub Desktop.
Generate pseudo-random passwords from a built in word list.
#!/usr/env python
'''
Author: Ben Hosmer
License: Unlicense http://unlicense.org/
Note: This is pseudo-random meaning don't rely on it for high-security!
'''
import random
import os
import sys
import linecache
import string
# The number of words to use from the list.
# Defaults to 2 if no value was given.
if len(sys.argv) > 1:
numofwords = int(sys.argv[1])
else:
numofwords = 2
platform = sys.platform
# OS X Default Word list
if platform == 'darwin':
if os.path.exists('/usr/share/dict/words'):
wordlist = '/usr/share/dict/words'
#print 'Using OS X Wordlist... ' + wordlist
# Linux Word List
elif platform == 'linux2':
if os.path.exists('/usr/share/dict/words'):
wordlist = '/usr/share/dict/words'
print 'linux wordlist found'
# Choose random words from the list to use.
with open(wordlist) as lines:
words = []
count = sum(1 for line in lines)
for i in range(numofwords):
words.append(linecache.getline(wordlist, random.randint(1, count)))
# Skip adding a special character to the first word, but add it to delimit
# each additional word.
words[1:] = [string.punctuation[random.randint(0, len(string.punctuation) -1)] + word for word in words[1:]]
# Clean up the list removing newlines and squash them all together in a string.
print ''.join([str(item.replace('\n', '')) for item in words])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment