Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 837f9e4

Browse files
authored
bpo-40645: Deprecated internal details of hmac.HMAC (GH-20132)
1 parent dff92bb commit 837f9e4

4 files changed

Lines changed: 59 additions & 23 deletions

File tree

Doc/library/hmac.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ A hash object has the following attributes:
114114
.. versionadded:: 3.4
115115

116116

117+
.. deprecated:: 3.9
118+
119+
The undocumented attributes ``HMAC.digest_cons``, ``HMAC.inner``, and
120+
``HMAC.outer`` are internal implementation details and will be removed in
121+
Python 3.10.
122+
117123
This module also provides the following helper function:
118124

119125
.. function:: compare_digest(a, b)

Lib/hmac.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class HMAC:
3030
"""
3131
blocksize = 64 # 512-bit HMAC; can be changed in subclasses.
3232

33+
__slots__ = (
34+
"_digest_cons", "_inner", "_outer", "block_size", "digest_size"
35+
)
36+
3337
def __init__(self, key, msg=None, digestmod=''):
3438
"""Create a new HMAC object.
3539
@@ -51,18 +55,18 @@ def __init__(self, key, msg=None, digestmod=''):
5155
raise TypeError("Missing required parameter 'digestmod'.")
5256

5357
if callable(digestmod):
54-
self.digest_cons = digestmod
58+
self._digest_cons = digestmod
5559
elif isinstance(digestmod, str):
56-
self.digest_cons = lambda d=b'': _hashlib.new(digestmod, d)
60+
self._digest_cons = lambda d=b'': _hashlib.new(digestmod, d)
5761
else:
58-
self.digest_cons = lambda d=b'': digestmod.new(d)
62+
self._digest_cons = lambda d=b'': digestmod.new(d)
5963

60-
self.outer = self.digest_cons()
61-
self.inner = self.digest_cons()
62-
self.digest_size = self.inner.digest_size
64+
self._outer = self._digest_cons()
65+
self._inner = self._digest_cons()
66+
self.digest_size = self._inner.digest_size
6367

64-
if hasattr(self.inner, 'block_size'):
65-
blocksize = self.inner.block_size
68+
if hasattr(self._inner, 'block_size'):
69+
blocksize = self._inner.block_size
6670
if blocksize < 16:
6771
_warnings.warn('block_size of %d seems too small; using our '
6872
'default of %d.' % (blocksize, self.blocksize),
@@ -79,21 +83,33 @@ def __init__(self, key, msg=None, digestmod=''):
7983
self.block_size = blocksize
8084

8185
if len(key) > blocksize:
82-
key = self.digest_cons(key).digest()
86+
key = self._digest_cons(key).digest()
8387

8488
key = key.ljust(blocksize, b'\0')
85-
self.outer.update(key.translate(trans_5C))
86-
self.inner.update(key.translate(trans_36))
89+
self._outer.update(key.translate(trans_5C))
90+
self._inner.update(key.translate(trans_36))
8791
if msg is not None:
8892
self.update(msg)
8993

9094
@property
9195
def name(self):
92-
return "hmac-" + self.inner.name
96+
return "hmac-" + self._inner.name
97+
98+
@property
99+
def digest_cons(self):
100+
return self._digest_cons
101+
102+
@property
103+
def inner(self):
104+
return self._inner
105+
106+
@property
107+
def outer(self):
108+
return self._outer
93109

94110
def update(self, msg):
95111
"""Feed data from msg into this hashing object."""
96-
self.inner.update(msg)
112+
self._inner.update(msg)
97113

98114
def copy(self):
99115
"""Return a separate copy of this hashing object.
@@ -102,19 +118,19 @@ def copy(self):
102118
"""
103119
# Call __new__ directly to avoid the expensive __init__.
104120
other = self.__class__.__new__(self.__class__)
105-
other.digest_cons = self.digest_cons
121+
other._digest_cons = self._digest_cons
106122
other.digest_size = self.digest_size
107-
other.inner = self.inner.copy()
108-
other.outer = self.outer.copy()
123+
other._inner = self._inner.copy()
124+
other._outer = self._outer.copy()
109125
return other
110126

111127
def _current(self):
112128
"""Return a hash object for the current state.
113129
114130
To be used only internally with digest() and hexdigest().
115131
"""
116-
h = self.outer.copy()
117-
h.update(self.inner.digest())
132+
h = self._outer.copy()
133+
h.update(self._inner.digest())
118134
return h
119135

120136
def digest(self):

Lib/test/test_hmac.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,11 @@ def test_attributes(self):
409409
# Testing if attributes are of same type.
410410
h1 = hmac.HMAC(b"key", digestmod="sha256")
411411
h2 = h1.copy()
412-
self.assertTrue(h1.digest_cons == h2.digest_cons,
412+
self.assertTrue(h1._digest_cons == h2._digest_cons,
413413
"digest constructors don't match.")
414-
self.assertEqual(type(h1.inner), type(h2.inner),
414+
self.assertEqual(type(h1._inner), type(h2._inner),
415415
"Types of inner don't match.")
416-
self.assertEqual(type(h1.outer), type(h2.outer),
416+
self.assertEqual(type(h1._outer), type(h2._outer),
417417
"Types of outer don't match.")
418418

419419
@hashlib_helper.requires_hashdigest('sha256')
@@ -423,10 +423,21 @@ def test_realcopy(self):
423423
h2 = h1.copy()
424424
# Using id() in case somebody has overridden __eq__/__ne__.
425425
self.assertTrue(id(h1) != id(h2), "No real copy of the HMAC instance.")
426-
self.assertTrue(id(h1.inner) != id(h2.inner),
426+
self.assertTrue(id(h1._inner) != id(h2._inner),
427427
"No real copy of the attribute 'inner'.")
428-
self.assertTrue(id(h1.outer) != id(h2.outer),
428+
self.assertTrue(id(h1._outer) != id(h2._outer),
429429
"No real copy of the attribute 'outer'.")
430+
self.assertEqual(h1._inner, h1.inner)
431+
self.assertEqual(h1._outer, h1.outer)
432+
self.assertEqual(h1._digest_cons, h1.digest_cons)
433+
434+
@hashlib_helper.requires_hashdigest('sha256')
435+
def test_properties(self):
436+
# deprecated properties
437+
h1 = hmac.HMAC(b"key", digestmod="sha256")
438+
self.assertEqual(h1._inner, h1.inner)
439+
self.assertEqual(h1._outer, h1.outer)
440+
self.assertEqual(h1._digest_cons, h1.digest_cons)
430441

431442
@hashlib_helper.requires_hashdigest('sha256')
432443
def test_equality(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The :class:`hmac.HMAC` exposes internal implementation details. The
2+
attributes ``digest_cons``, ``inner``, and ``outer`` are deprecated and will
3+
be removed in the future.

0 commit comments

Comments
 (0)