diff options
author | Christian Heimes <christian@cheimes.de> | 2013-11-20 16:23:06 (GMT) |
---|---|---|
committer | Christian Heimes <christian@cheimes.de> | 2013-11-20 16:23:06 (GMT) |
commit | 634919a9fa1fffe3d36b13b4248f99508b5999ed (patch) | |
tree | b60c9ca7e04ad97e11385712b3405fc9b42d7803 /Lib/hmac.py | |
parent | 7f48396cb5d19a40f571b0aec4916612f117a13d (diff) | |
download | cpython-634919a9fa1fffe3d36b13b4248f99508b5999ed.zip cpython-634919a9fa1fffe3d36b13b4248f99508b5999ed.tar.gz cpython-634919a9fa1fffe3d36b13b4248f99508b5999ed.tar.bz2 |
Issue #17276: MD5 as default digestmod for HMAC is deprecated. The HMAC
module supports digestmod names, e.g. hmac.HMAC('sha1').
Diffstat (limited to 'Lib/hmac.py')
-rw-r--r-- | Lib/hmac.py | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/Lib/hmac.py b/Lib/hmac.py index d13b205..873327b 100644 --- a/Lib/hmac.py +++ b/Lib/hmac.py @@ -5,6 +5,7 @@ Implements the HMAC algorithm as described by RFC 2104. import warnings as _warnings from _operator import _compare_digest as compare_digest +import hashlib as _hashlib trans_5C = bytes((x ^ 0x5C) for x in range(256)) trans_36 = bytes((x ^ 0x36) for x in range(256)) @@ -28,8 +29,11 @@ class HMAC: key: key for the keyed hash object. msg: Initial input for the hash, if provided. digestmod: A module supporting PEP 247. *OR* - A hashlib constructor returning a new hash object. + A hashlib constructor returning a new hash object. *OR* + A hash name suitable for hashlib.new(). Defaults to hashlib.md5. + Implicit default to hashlib.md5 is deprecated and will be + removed in Python 3.6. Note: key and msg must be a bytes or bytearray objects. """ @@ -38,11 +42,14 @@ class HMAC: raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__) if digestmod is None: - import hashlib - digestmod = hashlib.md5 + _warnings.warn("HMAC() without an explicit digestmod argument " + "is deprecated.", PendingDeprecationWarning, 2) + digestmod = _hashlib.md5 if callable(digestmod): self.digest_cons = digestmod + elif isinstance(digestmod, str): + self.digest_cons = lambda d=b'': _hashlib.new(digestmod, d) else: self.digest_cons = lambda d=b'': digestmod.new(d) |