]> granicus.if.org Git - python/commitdiff
Issue 18240: The HMAC module is no longer restricted to bytes and accepts
authorChristian Heimes <christian@cheimes.de>
Mon, 1 Jul 2013 11:08:42 +0000 (13:08 +0200)
committerChristian Heimes <christian@cheimes.de>
Mon, 1 Jul 2013 11:08:42 +0000 (13:08 +0200)
any bytes-like object, e.g. memoryview. Original patch by Jonas Borgström.

Doc/library/hmac.rst
Lib/hmac.py
Lib/test/test_hmac.py
Misc/ACKS
Misc/NEWS

index c2066a7bbdf310a85eb6111d545b0c09b11b2f29..4c707e950be891f30c5cb460f7b321f6210b7973 100644 (file)
@@ -16,20 +16,26 @@ This module implements the HMAC algorithm as described by :rfc:`2104`.
 
 .. function:: new(key, msg=None, digestmod=None)
 
-   Return a new hmac object.  *key* is a bytes object giving the secret key.  If
-   *msg* is present, the method call ``update(msg)`` is made.  *digestmod* is
-   the digest constructor or module for the HMAC object to use. It defaults to
-   the :func:`hashlib.md5` constructor.
+   Return a new hmac object.  *key* is a bytes or bytearray object giving the
+   secret key.  If *msg* is present, the method call ``update(msg)`` is made.
+   *digestmod* is the digest constructor or module for the HMAC object to use.
+   It defaults to the :func:`hashlib.md5` constructor.
 
+   .. versionchanged:: 3.4
+      Parameter *key* can be a bytes or bytearray object. Parameter *msg* can
+      be of any type supported by :mod:`hashlib`.
 
 An HMAC object has the following methods:
 
 .. method:: HMAC.update(msg)
 
-   Update the hmac object with the bytes object *msg*.  Repeated calls are
-   equivalent to a single call with the concatenation of all the arguments:
+   Update the hmac object with *msg*.  Repeated calls are equivalent to a
+   single call with the concatenation of all the arguments:
    ``m.update(a); m.update(b)`` is equivalent to ``m.update(a + b)``.
 
+   .. versionchanged:: 3.4
+      Parameter *msg* can be of any type supported by :mod:`hashlib`.
+
 
 .. method:: HMAC.digest()
 
index 6bd0de50236de9f23daa84e4ecc3814ef20edbdf..d13b205bbb9eeee98dc601cb2cdec949c9aeb670 100644 (file)
@@ -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):
index 4ca7cec44ce840214bb474dc083407619f20e0e0..efd63ad7be500903825c62da26b6aa258a81baf0 100644 (file)
@@ -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:
index 02c04bd11f2f830de93d98d575fb732dbb7290ea..ecf081e82ce91fe3ef22cfd27f337ef30eff5598 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -138,6 +138,7 @@ Gawain Bolton
 Forest Bond
 Gregory Bond
 Matias Bordese
+Jonas Borgström
 Jurjen Bos
 Peter Bosch
 Dan Boswell
index 137284be7e5a0546a525e9966fc501fafe73f04d..92d7c394ffe07d3055a78a121c34104fedf0de75 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -135,6 +135,9 @@ Core and Builtins
 Library
 -------
 
+- 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.
+
 - Issue #18224: Removed pydoc script from created venv, as it causes problems
   on Windows and adds no value over and above python -m pydoc ...