Created
October 13, 2011 19:31
-
-
Save emperorcezar/1285259 to your computer and use it in GitHub Desktop.
Generate a password using random words from words.txt
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
| import os | |
| import random | |
| class WordGenerator(object): | |
| def __init__(self): | |
| current_directory = os.path.dirname(os.path.abspath( __file__ )) | |
| words_file = open("%s/words.txt" % (current_directory), 'r') | |
| self.words = words_file.readlines() | |
| words_file.close() | |
| def random_word(self, min_length = 0): | |
| word = random.choice(self.words).strip() | |
| while len(word) < min_length: | |
| word = random.choice(self.words).strip() | |
| return word | |
| def generate_password(length = 8, chars = None, words = 3): | |
| generator = WordGenerator() | |
| password = '' | |
| for i in range(0, words): | |
| password += generator.random_word(min_length = length).capitalize() | |
| password += random.choice('0123456789') | |
| return password |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment