summaryrefslogtreecommitdiff
path: root/wsgitools
diff options
context:
space:
mode:
Diffstat (limited to 'wsgitools')
-rw-r--r--wsgitools/digest.py11
1 files changed, 7 insertions, 4 deletions
diff --git a/wsgitools/digest.py b/wsgitools/digest.py
index 2f49ff7..4f21af0 100644
--- a/wsgitools/digest.py
+++ b/wsgitools/digest.py
@@ -13,18 +13,21 @@ database using C{DBAPI2NonceStore}.
__all__ = []
-import random
import base64
import hashlib
import time
import os
+try:
+ from secrets import randbits
+except ImportError:
+ import random
+ sysrand = random.SystemRandom()
+ randbits = sysrand.getrandbits
from wsgitools.internal import bytes2str, str2bytes, textopen
from wsgitools.authentication import AuthenticationRequired, \
ProtocolViolation, AuthenticationMiddleware
-sysrand = random.SystemRandom()
-
def md5hex(data):
"""
@type data: str
@@ -41,7 +44,7 @@ def gen_rand_str(bytesentropy=33):
>>> gen_rand_str() != gen_rand_str()
True
"""
- randnum = sysrand.getrandbits(bytesentropy*8)
+ randnum = randbits(bytesentropy*8)
randstr = ("%%0%dX" % (2*bytesentropy)) % randnum
randbytes = str2bytes(randstr)
randbytes = base64.b16decode(randbytes)