Added code for fallback random generator use
authorSteven Tobin <tobin2402 (at) gmail.com>
Sat, 2 Jun 2012 09:52:03 +0000 (10:52 +0100)
committerSteven Tobin <tobin2402 (at) gmail.com>
Sat, 2 Jun 2012 09:52:03 +0000 (10:52 +0100)
Code attempts to use random.SystemRandom if present, will print a
warning then use random.Random() if not.

xkcd-password.py

index 23f7efc2da0b93b37e2b3aac6cc8296b0e6c18ee..fa3cb69322bcaa35376f9d89192744cdd97184be 100755 (executable)
@@ -35,11 +35,19 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 import random
 import os
-import sys
 import optparse
 import re
 import math
 
+# random.SystemRandom() should be cryptographically secure 
+try:
+    rng = random.SystemRandom
+except AttributeError:
+    print("WARNING: System does not support cryptographically secure random " 
+            "number generator or you are using Python version < 2.4. " 
+            "Continuing with less-secure generator.\n") 
+
+    rng = random.Random
 
 def generate_wordlist(wordfile=None,
                       min_length=5,
@@ -100,12 +108,7 @@ def generate_xkcdpassword(wordlist, n_words=4, interactive=False):
 
     # useful if driving the logic from other code
     if not interactive:
-        try:
-            # random.SystemRandom() should be cryptographically secure
-            return " ".join(random.SystemRandom().sample(wordlist, n_words))
-        except NotImplementedError:
-            print("System does not support random number generator or Python"
-                   "version < 2.4.")
+        return " ".join(rng().sample(wordlist, n_words))
 
     # else, interactive session
     custom_n_words = raw_input("Enter number of words (default 4): ")
@@ -116,11 +119,7 @@ def generate_xkcdpassword(wordlist, n_words=4, interactive=False):
     accepted = "n"
 
     while accepted.lower() not in ["y", "yes"]:
-        try:
-            passwd = " ".join(random.SystemRandom().sample(wordlist, n_words))
-        except NotImplementedError:
-            print("System does not support random number generator or Python"
-                   "version < 2.4.")
+        passwd = " ".join(rng().sample(wordlist, n_words))
         print("Generated: ", passwd)
         accepted = raw_input("Accept? [yN] ")
 
git clone https://git.99rst.org/PROJECT