summaryrefslogtreecommitdiff
path: root/passlib/utils
diff options
context:
space:
mode:
authorEli Collins <elic@assurancetechnologies.com>2011-02-16 15:46:59 -0500
committerEli Collins <elic@assurancetechnologies.com>2011-02-16 15:46:59 -0500
commit3eefcd1f31c534cced89139ebf72d36ceafc3a1c (patch)
treeb8805f986f236514429c54a1da9ad5d9bb989816 /passlib/utils
parentce0726991ad2756b890dee2bb3bcf8b72bc0123c (diff)
downloadpasslib-3eefcd1f31c534cced89139ebf72d36ceafc3a1c.tar.gz
passlib.utils:rng now uses SystemRandom if urandom available (previously just seeded a prng *from* urandom)
Diffstat (limited to 'passlib/utils')
-rw-r--r--passlib/utils/__init__.py13
1 files changed, 9 insertions, 4 deletions
diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py
index 516c5a8..896f5fc 100644
--- a/passlib/utils/__init__.py
+++ b/passlib/utils/__init__.py
@@ -310,6 +310,9 @@ except NotImplementedError:
def genseed(value=None):
"generate prng seed value from system resources"
+ #if value is rng, extract a bunch of bits from it's state
+ if hasattr(value, "getrandbits"):
+ value = value.getrandbits(256)
text = "%s %s %s %.15f %s" % (
value,
#if user specified a seed value (eg current rng state), mix it in
@@ -319,7 +322,7 @@ def genseed(value=None):
id(object()),
#id of a freshly created object.
- #(at least 2 bytes of which are hard to predict)
+ #(at least 2 bytes of which should be hard to predict)
time.time(),
#the current time, to whatever precision os uses
@@ -330,9 +333,11 @@ def genseed(value=None):
#hash it all up and return it as int
return long(sha256(text).hexdigest(), 16)
-rng = random.Random(genseed())
-
-#NOTE: to reseed rng: rng.seed(genseed(rng.getrandbits(32*8)))
+if has_urandom:
+ rng = random.SystemRandom()
+else:
+ #NOTE: to reseed - rng.seed(genseed(rng))
+ rng = random.Random(genseed())
#-----------------------------------------------------------------------
# some rng helpers