From 11e4968eb417459fac250665b9d274b4bb28f25a Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Sun, 26 Mar 2017 14:39:11 +0200 Subject: port wsgitools.digest to use the new secrets module --- wsgitools/digest.py | 11 +++++++---- 1 file 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) -- cgit v1.2.3