diff options
-rwxr-xr-x | wsgitools/digest.py | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/wsgitools/digest.py b/wsgitools/digest.py index 91ea8cf..c666460 100755 --- a/wsgitools/digest.py +++ b/wsgitools/digest.py @@ -12,6 +12,10 @@ import time sysrand = random.SystemRandom() +def gen_rand_str(bytes=33): + return (("%%0%dX" % (2*bytes)) % sysrand.getrandbits(bytes*8) + ).decode("hex").encode("base64").strip() + def parse_digest_response(data, ret=dict()): """internal @raises ValueError: @@ -161,8 +165,7 @@ class StatelessNonceStore(NonceStoreBase): if secret: self.server_secret = secret else: - self.server_secret = ("%066X" % sysrand.getrandbits(33*8) - ).decode("hex").encode("base64").strip() + self.server_secret = gen_rand_str() def newnonce(self, ident=None): """ @@ -170,8 +173,7 @@ class StatelessNonceStore(NonceStoreBase): @rtype: str """ nonce_time = format_time(time.time()) - nonce_value = ("%066X" % sysrand.getrandbits(33*8) - ).decode("hex").encode("base64").strip() + nonce_value = gen_rand_str() token = "%s:%s:%s" % (nonce_time, nonce_value, self.server_secret) if ident is not None: token = "%s:%s" % (token, ident) @@ -252,8 +254,7 @@ class MemoryNonceStore(NonceStoreBase): self.maxuses = maxuses self.nonces = [] # [(creation_time, nonce_value, useage_count)] # as [(float, str, int)] - self.server_secret = ("%066X" % sysrand.getrandbits(33*8) - ).decode("hex").encode("base64").strip() + self.server_secret = gen_rand_str() def _cleanup(self): """internal methods cleaning list of valid nonces""" @@ -268,8 +269,7 @@ class MemoryNonceStore(NonceStoreBase): """ self._cleanup() # avoid growing self.nonces nonce_time = format_time(time.time()) - nonce_value = ("%066X" % sysrand.getrandbits(33*8) - ).decode("hex").encode("base64").strip() + nonce_value = gen_rand_str() self.nonces.append((nonce_time, nonce_value, 1)) token = "%s:%s:%s" % (nonce_time, nonce_value, self.server_secret) if ident is not None: |