1- """HMAC (Keyed-Hashing for Message Authentication) Python module.
1+ """HMAC (Keyed-Hashing for Message Authentication) module.
22
33Implements the HMAC algorithm as described by RFC 2104.
44"""
@@ -30,23 +30,25 @@ class HMAC:
3030 """
3131 blocksize = 64 # 512-bit HMAC; can be changed in subclasses.
3232
33- def __init__ (self , key , msg = None , digestmod = None ):
33+ def __init__ (self , key , msg = None , digestmod = '' ):
3434 """Create a new HMAC object.
3535
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 .
4141
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.
4345 """
4446
4547 if not isinstance (key , (bytes , bytearray )):
4648 raise TypeError ("key: expected bytes or bytearray, but got %r" % type (key ).__name__ )
4749
48- if digestmod is None :
49- raise ValueError ( '`digestmod` is required.' )
50+ if not digestmod :
51+ raise TypeError ( "Missing required parameter 'digestmod'." )
5052
5153 if callable (digestmod ):
5254 self .digest_cons = digestmod
@@ -90,8 +92,7 @@ def name(self):
9092 return "hmac-" + self .inner .name
9193
9294 def update (self , msg ):
93- """Update this hashing object with the string msg.
94- """
95+ """Feed data from msg into this hashing object."""
9596 self .inner .update (msg )
9697
9798 def copy (self ):
@@ -119,7 +120,7 @@ def _current(self):
119120 def digest (self ):
120121 """Return the hash value of this hashing object.
121122
122- This returns a string containing 8-bit data . The object is
123+ This returns the hmac value as bytes . The object is
123124 not altered in any way by this function; you can continue
124125 updating the object after calling this function.
125126 """
@@ -132,30 +133,34 @@ def hexdigest(self):
132133 h = self ._current ()
133134 return h .hexdigest ()
134135
135- def new (key , msg = None , digestmod = None ):
136+ def new (key , msg = None , digestmod = '' ):
136137 """Create a new hashing object and return it.
137138
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.
141148
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()
143150 method, and can ask for the hash value at any time by calling its digest()
144- method .
151+ or hexdigest() methods .
145152 """
146153 return HMAC (key , msg , digestmod )
147154
148155
149156def digest (key , msg , digest ):
150- """Fast inline implementation of HMAC
157+ """Fast inline implementation of HMAC.
151158
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.
154161 digest: A hash name suitable for hashlib.new() for best performance. *OR*
155162 A hashlib constructor returning a new hash object. *OR*
156163 A module supporting PEP 247.
157-
158- Note: key and msg must be a bytes or bytearray objects.
159164 """
160165 if (_hashopenssl is not None and
161166 isinstance (digest , str ) and digest in _openssl_md_meths ):
0 commit comments