summaryrefslogtreecommitdiffstats
path: root/Lib/hmac.py
diff options
context:
space:
mode:
authorGregory P. Smith <greg@mad-scientist.com>2005-08-21 18:45:59 (GMT)
committerGregory P. Smith <greg@mad-scientist.com>2005-08-21 18:45:59 (GMT)
commitf21a5f773964d34c7b6deb7e3d753fae2b9c70e2 (patch)
treeba3b66cea11da1d8e930555aa5a10f775a285d84 /Lib/hmac.py
parent33a5f2af59ddcf3f1b0447a8dbd0576fd78de303 (diff)
downloadcpython-f21a5f773964d34c7b6deb7e3d753fae2b9c70e2.zip
cpython-f21a5f773964d34c7b6deb7e3d753fae2b9c70e2.tar.gz
cpython-f21a5f773964d34c7b6deb7e3d753fae2b9c70e2.tar.bz2
[ sf.net patch # 1121611 ]
A new hashlib module to replace the md5 and sha modules. It adds support for additional secure hashes such as SHA-256 and SHA-512. The hashlib module uses OpenSSL for fast platform optimized implementations of algorithms when available. The old md5 and sha modules still exist as wrappers around hashlib to preserve backwards compatibility.
Diffstat (limited to 'Lib/hmac.py')
-rw-r--r--Lib/hmac.py24
1 files changed, 15 insertions, 9 deletions
diff --git a/Lib/hmac.py b/Lib/hmac.py
index 11b0fb3..41d6c6c 100644
--- a/Lib/hmac.py
+++ b/Lib/hmac.py
@@ -28,27 +28,33 @@ class HMAC:
key: key for the keyed hash object.
msg: Initial input for the hash, if provided.
- digestmod: A module supporting PEP 247. Defaults to the md5 module.
+ digestmod: A module supporting PEP 247. *OR*
+ A hashlib constructor returning a new hash object.
+ Defaults to hashlib.md5.
"""
if key is _secret_backdoor_key: # cheap
return
if digestmod is None:
- import md5
- digestmod = md5
+ import hashlib
+ digestmod = hashlib.md5
- self.digestmod = digestmod
- self.outer = digestmod.new()
- self.inner = digestmod.new()
- self.digest_size = digestmod.digest_size
+ if callable(digestmod):
+ self.digest_cons = digestmod
+ else:
+ self.digest_cons = lambda d='': digestmod.new(d)
+
+ self.outer = self.digest_cons()
+ self.inner = self.digest_cons()
+ self.digest_size = self.inner.digest_size
blocksize = 64
ipad = "\x36" * blocksize
opad = "\x5C" * blocksize
if len(key) > blocksize:
- key = digestmod.new(key).digest()
+ key = self.digest_cons(key).digest()
key = key + chr(0) * (blocksize - len(key))
self.outer.update(_strxor(key, opad))
@@ -70,7 +76,7 @@ class HMAC:
An update to this copy won't affect the original object.
"""
other = HMAC(_secret_backdoor_key)
- other.digestmod = self.digestmod
+ other.digest_cons = self.digest_cons
other.digest_size = self.digest_size
other.inner = self.inner.copy()
other.outer = self.outer.copy()