From: Rob Lanphier Date: Sun, 20 Nov 2011 04:52:47 +0000 (-0800) Subject: Added --valid_chars option X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=0aea84fd20c7fecb2142dada06c9990ba9985278;p=redacted-XKCD-password-generator.git Added --valid_chars option --- diff --git a/xkcd-password.py b/xkcd-password.py index b4ab5fd..799a919 100755 --- a/xkcd-password.py +++ b/xkcd-password.py @@ -32,8 +32,9 @@ import random import os import sys import optparse +import re -def generate_wordlist(wordfile=None, min_length=5, max_length=9): +def generate_wordlist(wordfile=None, min_length=5, max_length=9, valid_chars='.'): """ generate a word list from either a kwarg word_file, or a system default """ @@ -53,12 +54,13 @@ def generate_wordlist(wordfile=None, min_length=5, max_length=9): wordfile = os.path.expanduser(wordfile) # just to be sure words = [] - + regexp=re.compile('^%s{%i,%i}$' % (valid_chars, min_length, max_length)) try: with open(wordfile) as wlf: for line in wlf: - if min_length <= len(line.strip()) <= max_length: - words.append(line.strip()) + thisword=line.strip() + if regexp.match(thisword) is not None: + words.append(thisword) except: print "Word list not loaded" raise SystemExit @@ -113,6 +115,9 @@ if __name__ == '__main__': parser.add_option("-i", "--interactive", dest="interactive", default=False, action="store_true", help="Interactively select a password") + parser.add_option("-v", "--valid_chars", dest="valid_chars", + default='.', + help="Valid chars, using regexp style (e.g. '[a-z]'") (options, args) = parser.parse_args() @@ -128,6 +133,7 @@ if __name__ == '__main__': my_wordlist = generate_wordlist(wordfile=options.wordfile, min_length=options.min_length, - max_length=options.max_length) + max_length=options.max_length, + valid_chars=options.valid_chars) print generate_xkcdpassword(my_wordlist, interactive=options.interactive)