summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2017-03-26 14:39:11 +0200
committerHelmut Grohne <helmut@subdivi.de>2017-03-26 14:39:11 +0200
commit11e4968eb417459fac250665b9d274b4bb28f25a (patch)
tree289154466075a1cf98760023532adf08ade868ca
parent184fbb78ec3153d9d9f3ac410494dfa5d1656bd1 (diff)
downloadwsgitools-11e4968eb417459fac250665b9d274b4bb28f25a.tar.gz
port wsgitools.digest to use the new secrets module
-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)