bpo-33604: Raise TypeError on missing hmac arg. (GH-16805) · python/cpython@f33c57d (original) (raw)
1
``
`-
"""HMAC (Keyed-Hashing for Message Authentication) Python module.
`
``
1
`+
"""HMAC (Keyed-Hashing for Message Authentication) module.
`
2
2
``
3
3
`Implements the HMAC algorithm as described by RFC 2104.
`
4
4
`"""
`
`@@ -30,23 +30,25 @@ class HMAC:
`
30
30
` """
`
31
31
`blocksize = 64 # 512-bit HMAC; can be changed in subclasses.
`
32
32
``
33
``
`-
def init(self, key, msg = None, digestmod = None):
`
``
33
`+
def init(self, key, msg=None, digestmod=''):
`
34
34
`"""Create a new HMAC object.
`
35
35
``
36
``
`-
key: key for the keyed hash object.
`
37
``
`-
msg: Initial input for the hash, if provided.
`
38
``
`-
digestmod: Required. A module supporting PEP 247. OR
`
39
``
`-
A hashlib constructor returning a new hash object. OR
`
40
``
`-
A hash name suitable for hashlib.new().
`
``
36
`+
key: bytes or buffer, key for the keyed hash object.
`
``
37
`+
msg: bytes or buffer, Initial input for the hash or None.
`
``
38
`+
digestmod: A hash name suitable for hashlib.new(). OR
`
``
39
`+
A hashlib constructor returning a new hash object. OR
`
``
40
`+
A module supporting PEP 247.
`
41
41
``
42
``
`-
Note: key and msg must be a bytes or bytearray objects.
`
``
42
`+
Required as of 3.8, despite its position after the optional
`
``
43
`+
msg argument. Passing it as a keyword argument is
`
``
44
`+
recommended, though not required for legacy API reasons.
`
43
45
` """
`
44
46
``
45
47
`if not isinstance(key, (bytes, bytearray)):
`
46
48
`raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).name)
`
47
49
``
48
``
`-
if digestmod is None:
`
49
``
`` -
raise ValueError('digestmod
is required.')
``
``
50
`+
if not digestmod:
`
``
51
`+
raise TypeError("Missing required parameter 'digestmod'.")
`
50
52
``
51
53
`if callable(digestmod):
`
52
54
`self.digest_cons = digestmod
`
`@@ -90,8 +92,7 @@ def name(self):
`
90
92
`return "hmac-" + self.inner.name
`
91
93
``
92
94
`def update(self, msg):
`
93
``
`-
"""Update this hashing object with the string msg.
`
94
``
`-
"""
`
``
95
`+
"""Feed data from msg into this hashing object."""
`
95
96
`self.inner.update(msg)
`
96
97
``
97
98
`def copy(self):
`
`@@ -119,7 +120,7 @@ def _current(self):
`
119
120
`def digest(self):
`
120
121
`"""Return the hash value of this hashing object.
`
121
122
``
122
``
`-
This returns a string containing 8-bit data. The object is
`
``
123
`+
This returns the hmac value as bytes. The object is
`
123
124
` not altered in any way by this function; you can continue
`
124
125
` updating the object after calling this function.
`
125
126
` """
`
`@@ -132,30 +133,34 @@ def hexdigest(self):
`
132
133
`h = self._current()
`
133
134
`return h.hexdigest()
`
134
135
``
135
``
`-
def new(key, msg = None, digestmod = None):
`
``
136
`+
def new(key, msg=None, digestmod=''):
`
136
137
`"""Create a new hashing object and return it.
`
137
138
``
138
``
`-
key: The starting key for the hash.
`
139
``
`-
msg: if available, will immediately be hashed into the object's starting
`
140
``
`-
state.
`
``
139
`+
key: bytes or buffer, The starting key for the hash.
`
``
140
`+
msg: bytes or buffer, Initial input for the hash, or None.
`
``
141
`+
digestmod: A hash name suitable for hashlib.new(). OR
`
``
142
`+
A hashlib constructor returning a new hash object. OR
`
``
143
`+
A module supporting PEP 247.
`
``
144
+
``
145
`+
Required as of 3.8, despite its position after the optional
`
``
146
`+
msg argument. Passing it as a keyword argument is
`
``
147
`+
recommended, though not required for legacy API reasons.
`
141
148
``
142
``
`-
You can now feed arbitrary strings into the object using its update()
`
``
149
`+
You can now feed arbitrary bytes into the object using its update()
`
143
150
` method, and can ask for the hash value at any time by calling its digest()
`
144
``
`-
method.
`
``
151
`+
or hexdigest() methods.
`
145
152
` """
`
146
153
`return HMAC(key, msg, digestmod)
`
147
154
``
148
155
``
149
156
`def digest(key, msg, digest):
`
150
``
`-
"""Fast inline implementation of HMAC
`
``
157
`+
"""Fast inline implementation of HMAC.
`
151
158
``
152
``
`-
key: key for the keyed hash object.
`
153
``
`-
msg: input message
`
``
159
`+
key: bytes or buffer, The key for the keyed hash object.
`
``
160
`+
msg: bytes or buffer, Input message.
`
154
161
` digest: A hash name suitable for hashlib.new() for best performance. OR
`
155
162
` A hashlib constructor returning a new hash object. OR
`
156
163
` A module supporting PEP 247.
`
157
``
-
158
``
`-
Note: key and msg must be a bytes or bytearray objects.
`
159
164
` """
`
160
165
`if (_hashopenssl is not None and
`
161
166
`isinstance(digest, str) and digest in _openssl_md_meths):
`