["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)
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,
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
def generate_xkcdpassword(wordlist,
n_words=4,
interactive=False,
- acrostic=False):
+ acrostic=False,
+ delim=" "):
"""
Generate an XKCD-style password from the words in 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:
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] ")
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)
print(generate_xkcdpassword(my_wordlist,
interactive=options.interactive,
n_words=options.numwords,
- acrostic=options.acrostic))
+ acrostic=options.acrostic,
+ delim=options.delim))
count -= 1