summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDwayne Litzenberger <dlitz@dlitz.net>2013-09-29 17:41:13 -0700
committerDwayne Litzenberger <dlitz@dlitz.net>2013-10-20 17:36:06 -0700
commit141eee1093bc9adbbdc3b3b3e90bde868d46eeec (patch)
treedc52fb9933535df3f2bc46148357d392c8103743
parent1fc5c01f197e2bec97b43020ebd0c3176a2acc95 (diff)
downloadpycrypto-141eee1093bc9adbbdc3b3b3e90bde868d46eeec.tar.gz
hexverify: Fix handling unicode strings on Python 3.2
We were getting this error on Python 3.2: ERROR: runTest (Crypto.SelfTest.Hash.common.MACSelfTest) CMAC #17: NIST SP 800 38B D.7 Example 17 ---------------------------------------------------------------------- Traceback (most recent call last): File "build/lib.linux-x86_64-3.2/Crypto/SelfTest/Hash/common.py", line 199, in runTest self.assertRaises(ValueError, h.hexverify, "4556") File "/home/dwon/py/pythons/python3.2/lib/python3.2/unittest/case.py", line 557, in assertRaises callableObj(*args, **kwargs) File "build/lib.linux-x86_64-3.2/Crypto/Hash/CMAC.py", line 323, in hexverify self.verify(unhexlify(hex_mac_tag)) TypeError: 'str' does not support the buffer interface
-rw-r--r--lib/Crypto/Hash/CMAC.py2
-rw-r--r--lib/Crypto/Hash/HMAC.py2
-rw-r--r--lib/Crypto/SelfTest/Hash/common.py12
3 files changed, 13 insertions, 3 deletions
diff --git a/lib/Crypto/Hash/CMAC.py b/lib/Crypto/Hash/CMAC.py
index 107db96..6305054 100644
--- a/lib/Crypto/Hash/CMAC.py
+++ b/lib/Crypto/Hash/CMAC.py
@@ -320,7 +320,7 @@ class CMAC(_SmoothMAC):
has been tampered with or that the MAC key is incorrect.
"""
- self.verify(unhexlify(hex_mac_tag))
+ self.verify(unhexlify(tobytes(hex_mac_tag)))
def new(key, msg = None, ciphermod = None):
"""Create a new CMAC object.
diff --git a/lib/Crypto/Hash/HMAC.py b/lib/Crypto/Hash/HMAC.py
index b44a082..8865411 100644
--- a/lib/Crypto/Hash/HMAC.py
+++ b/lib/Crypto/Hash/HMAC.py
@@ -238,7 +238,7 @@ class HMAC:
has been tampered with or that the MAC key is incorrect.
"""
- self.verify(unhexlify(hex_mac_tag))
+ self.verify(unhexlify(tobytes(hex_mac_tag)))
def new(key, msg = None, digestmod = None):
"""Create a new HMAC object.
diff --git a/lib/Crypto/SelfTest/Hash/common.py b/lib/Crypto/SelfTest/Hash/common.py
index 968caa2..2b3cbc3 100644
--- a/lib/Crypto/SelfTest/Hash/common.py
+++ b/lib/Crypto/SelfTest/Hash/common.py
@@ -206,7 +206,17 @@ class MACSelfTest(unittest.TestCase):
h.update(b("blah blah blah")) # Corrupt the original hash object
out5 = binascii.b2a_hex(h2.digest()) # The copied hash object should return the correct result
- # PY3K: hexdigest() should return str(), and digest() bytes
+ # PY3K: Check that hexdigest() returns str and digest() returns bytes
+ if sys.version_info[0] > 2:
+ self.assertTrue(isinstance(h.digest(), type(b(""))))
+ self.assertTrue(isinstance(h.hexdigest(), type("")))
+
+ # PY3K: Check that .hexverify() accepts bytes or str
+ if sys.version_info[0] > 2:
+ h.hexverify(h.hexdigest())
+ h.hexverify(h.hexdigest().encode('ascii'))
+
+ # PY3K: hexdigest() should return str, and digest() should return bytes
self.assertEqual(expected, out1)
if sys.version_info[0] == 2:
self.assertEqual(expected, out2)