Allow duplicate words in passphrases
authorLars Noschinski <redacted>
Mon, 26 Oct 2015 13:13:04 +0000 (14:13 +0100)
committerLars Noschinski <redacted>
Mon, 26 Oct 2015 16:00:24 +0000 (17:00 +0100)
xkcdpass/xkcd_password.py

index b6f2da026ec54f5e2165359add620d1675341084..9d06da6ea652d576bd7ee3e2778829625e6f39d7 100755 (executable)
@@ -60,6 +60,7 @@ except AttributeError:
 # Python 3 compatibility
 if sys.version_info[0] >= 3:
     raw_input = input
+    xrange = range
 
 
 def validate_options(parser, options, args):
@@ -199,6 +200,12 @@ def find_acrostic(acrostic, worddict):
     return words
 
 
+def choose_words(wordlist, numwords):
+    s = []
+    for i in xrange(numwords):
+        s.append(rng().choice(wordlist))
+    return s
+
 def generate_xkcdpassword(wordlist,
                           numwords=6,
                           interactive=False,
@@ -210,12 +217,6 @@ def generate_xkcdpassword(wordlist,
 
     passwd = False
 
-    if len(wordlist) < numwords:
-        sys.stderr.write("Could not get enough words!\n"
-                         "This could be a result of either your wordfile\n"
-                         "being too small, or your settings too strict.\n")
-        sys.exit(1)
-
     # generate the worddict if we are looking for acrostics
     if acrostic:
         worddict = wordlist_to_worddict(wordlist)
@@ -223,7 +224,7 @@ def generate_xkcdpassword(wordlist,
     # useful if driving the logic from other code
     if not interactive:
         if not acrostic:
-            passwd = delimiter.join(rng().sample(wordlist, numwords))
+            passwd = delimiter.join(choose_words(wordlist, numwords))
         else:
             passwd = delimiter.join(find_acrostic(acrostic, worddict))
 
@@ -242,7 +243,7 @@ def generate_xkcdpassword(wordlist,
 
     while accepted.lower() not in ["y", "yes"]:
         if not acrostic:
-            passwd = delimiter.join(rng().sample(wordlist, numwords))
+            passwd = delimiter.join(choose_words(wordlist, numwords))
         else:
             passwd = delimiter.join(find_acrostic(acrostic, worddict))
         print("Generated: ", passwd)
git clone https://git.99rst.org/PROJECT