diff options
author | Helmut Grohne <helmut@subdivi.de> | 2009-02-27 11:50:35 +0100 |
---|---|---|
committer | Helmut Grohne <helmut@subdivi.de> | 2009-02-27 11:50:35 +0100 |
commit | f6603653ff922bc84c48717e28392e88becbb8e0 (patch) | |
tree | 85b3e426dc3b4929152449a2be98af06b4b31141 | |
parent | a3f5b782fded63de63641f3e411301eb6ab96dfb (diff) | |
download | wsgitools-f6603653ff922bc84c48717e28392e88becbb8e0.tar.gz |
extract common code to gen_rand_str in digest
-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: |