Skip to content

Commit 51a4743

Browse files
Carreaugpshead
authored andcommitted
bpo-33604: Remove deprecated HMAC default value marked for removal in 3.8 (GH-7063)
HMAC's digestmod was deprecated marked for removal, this removes it as planned.
1 parent 78deb7f commit 51a4743

File tree

4 files changed

+13
-33
lines changed

4 files changed

+13
-33
lines changed

Doc/library/hmac.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ This module implements the HMAC algorithm as described by :rfc:`2104`.
1919
Return a new hmac object. *key* is a bytes or bytearray object giving the
2020
secret key. If *msg* is present, the method call ``update(msg)`` is made.
2121
*digestmod* is the digest name, digest constructor or module for the HMAC
22-
object to use. It supports any name suitable to :func:`hashlib.new` and
23-
defaults to the :data:`hashlib.md5` constructor.
22+
object to use. It supports any name suitable to :func:`hashlib.new`.
2423

2524
.. versionchanged:: 3.4
2625
Parameter *key* can be a bytes or bytearray object.

Lib/hmac.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,9 @@ def __init__(self, key, msg = None, digestmod = None):
3535
3636
key: key for the keyed hash object.
3737
msg: Initial input for the hash, if provided.
38-
digestmod: A module supporting PEP 247. *OR*
39-
A hashlib constructor returning a new hash object. *OR*
38+
digestmod: Required. A module supporting PEP 247. *OR*
39+
A hashlib constructor returning a new hash object. *OR*
4040
A hash name suitable for hashlib.new().
41-
Defaults to hashlib.md5.
42-
Implicit default to hashlib.md5 is deprecated since Python
43-
3.4 and will be removed in Python 3.8.
4441
4542
Note: key and msg must be a bytes or bytearray objects.
4643
"""
@@ -49,11 +46,7 @@ def __init__(self, key, msg = None, digestmod = None):
4946
raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__)
5047

5148
if digestmod is None:
52-
_warnings.warn("HMAC() without an explicit digestmod argument "
53-
"is deprecated since Python 3.4, and will be removed "
54-
"in 3.8",
55-
DeprecationWarning, 2)
56-
digestmod = _hashlib.md5
49+
raise ValueError('`digestmod` is required.')
5750

5851
if callable(digestmod):
5952
self.digest_cons = digestmod

Lib/test/test_hmac.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -302,45 +302,38 @@ def digest(self):
302302
hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash)
303303
self.fail('Expected warning about small block_size')
304304

305-
def test_with_digestmod_warning(self):
306-
with self.assertWarns(DeprecationWarning):
305+
def test_with_digestmod_no_default(self):
306+
with self.assertRaises(ValueError):
307307
key = b"\x0b" * 16
308308
data = b"Hi There"
309-
digest = "9294727A3638BB1C13F48EF8158BFC9D"
310-
h = hmac.HMAC(key, data)
311-
self.assertEqual(h.hexdigest().upper(), digest)
312-
309+
hmac.HMAC(key, data, digestmod=None)
313310

314311
class ConstructorTestCase(unittest.TestCase):
315312

316-
@ignore_warning
317313
def test_normal(self):
318314
# Standard constructor call.
319315
failed = 0
320316
try:
321-
h = hmac.HMAC(b"key")
317+
h = hmac.HMAC(b"key", digestmod='md5')
322318
except Exception:
323319
self.fail("Standard constructor call raised exception.")
324320

325-
@ignore_warning
326321
def test_with_str_key(self):
327322
# Pass a key of type str, which is an error, because it expects a key
328323
# of type bytes
329324
with self.assertRaises(TypeError):
330-
h = hmac.HMAC("key")
325+
h = hmac.HMAC("key", digestmod='md5')
331326

332-
@ignore_warning
333327
def test_dot_new_with_str_key(self):
334328
# Pass a key of type str, which is an error, because it expects a key
335329
# of type bytes
336330
with self.assertRaises(TypeError):
337-
h = hmac.new("key")
331+
h = hmac.new("key", digestmod='md5')
338332

339-
@ignore_warning
340333
def test_withtext(self):
341334
# Constructor call with text.
342335
try:
343-
h = hmac.HMAC(b"key", b"hash this!")
336+
h = hmac.HMAC(b"key", b"hash this!", digestmod='md5')
344337
except Exception:
345338
self.fail("Constructor call with text argument raised exception.")
346339
self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864')
@@ -369,13 +362,6 @@ def test_withmodule(self):
369362

370363
class SanityTestCase(unittest.TestCase):
371364

372-
@ignore_warning
373-
def test_default_is_md5(self):
374-
# Testing if HMAC defaults to MD5 algorithm.
375-
# NOTE: this whitebox test depends on the hmac class internals
376-
h = hmac.HMAC(b"key")
377-
self.assertEqual(h.digest_cons, hashlib.md5)
378-
379365
def test_exercise_all_methods(self):
380366
# Exercising all methods once.
381367
# This must not raise any exceptions
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Remove HMAC default to md5 marked for removal in 3.8 (removal originally
2+
planned in 3.6, bump to 3.8 in gh-7062).

0 commit comments

Comments
 (0)