From 52e95798b4fe36e393ca7a90678f12ff0233c96a Mon Sep 17 00:00:00 2001 From: Bob Halley Date: Thu, 4 Aug 2016 06:03:31 -0700 Subject: [PATCH] Use random.SystemRandom when available. --- dns/entropy.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/dns/entropy.py b/dns/entropy.py index 22114579..7a2d87d1 100644 --- a/dns/entropy.py +++ b/dns/entropy.py @@ -14,6 +14,7 @@ # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. import os +import random import time from ._compat import long, binary_type try: @@ -122,10 +123,19 @@ class EntropyPool(object): pool = EntropyPool() +try: + system_random = random.SystemRandom() +except: + system_random = None def random_16(): - return pool.random_16() - + if system_random is not None: + return system_random.randrange(0, 65536) + else: + return pool.random_16() def between(first, last): - return pool.random_between(first, last) + if system_random is not None: + return system_random.randrange(first, last + 1) + else: + return pool.random_between(first, last) -- 2.47.3