From: Steven Tobin Date: Fri, 14 Jun 2013 01:03:02 +0000 (+0100) Subject: changes to delimiter code X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=3665a9308dda90a2e4fb39b105cb4f6b9c5ca12d;p=redacted-XKCD-password-generator.git changes to delimiter code --- diff --git a/tests/xkcdp_tests.py b/tests/xkcdp_tests.py index c6b1600..95fd4d0 100644 --- a/tests/xkcdp_tests.py +++ b/tests/xkcdp_tests.py @@ -32,6 +32,16 @@ class XkcdPasswordTests(unittest.TestCase): ["python", "xkcd_password.py", "-w", "3esl.txt", "-c", str(count)]) self.assertTrue(result.count("\n"), count) + def test_delim(self): + tdelim = "_" + target = tdelim.join(["factual", "amazing", "captain", "exactly"]) + # use an acrostic for simpler target check + result = xkcd_password.generate_xkcdpassword( + self.wordlist_small, + acrostic="face", + delim=tdelim) + self.assertEquals(result, target) + if __name__ == '__main__': suite = unittest.TestLoader().loadTestsFromTestCase(XkcdPasswordTests) diff --git a/xkcd_password.py b/xkcd_password.py index 05e3dd1..585f63e 100755 --- a/xkcd_password.py +++ b/xkcd_password.py @@ -97,7 +97,7 @@ def validate_options(options, args): sys.stderr.write("Could not find a word file, or word file does " "not exist.\n") sys.exit(1) - + def generate_wordlist(wordfile=None, min_length=5, @@ -155,12 +155,12 @@ def find_acrostic(acrostic, wordlist): given word (acrostic). """ - words = "" + words = [] for letter in acrostic: while 1: word = rng().choice(wordlist) if word[0] == letter: - words += word + options.delim + words.append(word) break return words @@ -168,7 +168,8 @@ def find_acrostic(acrostic, wordlist): def generate_xkcdpassword(wordlist, n_words=4, interactive=False, - acrostic=False): + acrostic=False, + delim=" "): """ Generate an XKCD-style password from the words in wordlist. """ @@ -182,9 +183,9 @@ def generate_xkcdpassword(wordlist, # useful if driving the logic from other code if not interactive: if not acrostic: - return options.delim.join(rng().sample(wordlist, n_words)) + return delim.join(rng().sample(wordlist, n_words)) else: - return find_acrostic(acrostic, wordlist) + return delim.join(find_acrostic(acrostic, wordlist)) # else, interactive session if not acrostic: @@ -199,9 +200,9 @@ def generate_xkcdpassword(wordlist, while accepted.lower() not in ["y", "yes"]: if not acrostic: - passwd = options.delim.join(rng().sample(wordlist, n_words)) + passwd = delim.join(rng().sample(wordlist, n_words)) else: - passwd = find_acrostic(acrostic, wordlist) + passwd = delim.join(find_acrostic(acrostic, wordlist)) print("Generated: ", passwd) accepted = raw_input("Accept? [yN] ") @@ -242,7 +243,9 @@ if __name__ == '__main__': default=1, type="int", help="number of passwords to generate") parser.add_option("-d", "--delimiter", dest="delim", - default=" ", help="separator character between words") + default=" ", + help="separator character between words") + (options, args) = parser.parse_args() validate_options(options, args) @@ -261,5 +264,6 @@ if __name__ == '__main__': print(generate_xkcdpassword(my_wordlist, interactive=options.interactive, n_words=options.numwords, - acrostic=options.acrostic)) + acrostic=options.acrostic, + delim=options.delim)) count -= 1