diff options
author | Christian Heimes <christian@cheimes.de> | 2013-07-01 11:08:42 (GMT) |
---|---|---|
committer | Christian Heimes <christian@cheimes.de> | 2013-07-01 11:08:42 (GMT) |
commit | 04926aeb2f88c39a25505e4a0474c6fb735e0f46 (patch) | |
tree | e2f8f58f84ec61dfb533a58320af98409c9b5c57 /Lib | |
parent | ec4bdac8dd8c1a2fb6c211046dd0fca47a9896f2 (diff) | |
download | cpython-04926aeb2f88c39a25505e4a0474c6fb735e0f46.zip cpython-04926aeb2f88c39a25505e4a0474c6fb735e0f46.tar.gz cpython-04926aeb2f88c39a25505e4a0474c6fb735e0f46.tar.bz2 |
Issue 18240: The HMAC module is no longer restricted to bytes and accepts
any bytes-like object, e.g. memoryview. Original patch by Jonas Borgström.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/hmac.py | 8 | ||||
-rw-r--r-- | Lib/test/test_hmac.py | 14 |
2 files changed, 17 insertions, 5 deletions
diff --git a/Lib/hmac.py b/Lib/hmac.py index 6bd0de5..d13b205 100644 --- a/Lib/hmac.py +++ b/Lib/hmac.py @@ -31,11 +31,11 @@ class HMAC: A hashlib constructor returning a new hash object. Defaults to hashlib.md5. - Note: key and msg must be bytes objects. + Note: key and msg must be a bytes or bytearray objects. """ - if not isinstance(key, bytes): - raise TypeError("key: expected bytes, but got %r" % type(key).__name__) + if not isinstance(key, (bytes, bytearray)): + raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__) if digestmod is None: import hashlib @@ -75,8 +75,6 @@ class HMAC: def update(self, msg): """Update this hashing object with the string msg. """ - if not isinstance(msg, bytes): - raise TypeError("expected bytes, but got %r" % type(msg).__name__) self.inner.update(msg) def copy(self): diff --git a/Lib/test/test_hmac.py b/Lib/test/test_hmac.py index 4ca7cec..efd63ad 100644 --- a/Lib/test/test_hmac.py +++ b/Lib/test/test_hmac.py @@ -253,6 +253,20 @@ class ConstructorTestCase(unittest.TestCase): except: self.fail("Constructor call with text argument raised exception.") + def test_with_bytearray(self): + try: + h = hmac.HMAC(bytearray(b"key"), bytearray(b"hash this!")) + self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864') + except: + self.fail("Constructor call with bytearray arguments raised exception.") + + def test_with_memoryview_msg(self): + try: + h = hmac.HMAC(b"key", memoryview(b"hash this!")) + self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864') + except: + self.fail("Constructor call with memoryview msg raised exception.") + def test_withmodule(self): # Constructor call with text and digest module. try: |