Reverted type check added in 62954ba04c.

Refs #17040.
This commit is contained in:
Aymeric Augustin 2012-08-21 08:44:16 +02:00
parent 831f2846dd
commit e89bc39935
2 changed files with 4 additions and 6 deletions

View File

@ -82,16 +82,14 @@ def get_random_string(length=12,
def constant_time_compare(val1, val2): def constant_time_compare(val1, val2):
""" """
Returns True if the two bytestrings are equal, False otherwise. Returns True if the two strings are equal, False otherwise.
The time taken is independent of the number of characters that match. The time taken is independent of the number of characters that match.
""" """
if not (isinstance(val1, bytes) and isinstance(val2, bytes)):
raise TypeError("constant_time_compare only supports bytes")
if len(val1) != len(val2): if len(val1) != len(val2):
return False return False
result = 0 result = 0
if six.PY3: if six.PY3 and isinstance(val1, bytes) and isinstance(val2, bytes):
for x, y in zip(val1, val2): for x, y in zip(val1, val2):
result |= x ^ y result |= x ^ y
else: else:

View File

@ -15,8 +15,8 @@ class TestUtilsCryptoMisc(unittest.TestCase):
# It's hard to test for constant time, just test the result. # It's hard to test for constant time, just test the result.
self.assertTrue(constant_time_compare(b'spam', b'spam')) self.assertTrue(constant_time_compare(b'spam', b'spam'))
self.assertFalse(constant_time_compare(b'spam', b'eggs')) self.assertFalse(constant_time_compare(b'spam', b'eggs'))
with self.assertRaises(TypeError): self.assertTrue(constant_time_compare('spam', 'spam'))
constant_time_compare('spam', 'spam') self.assertFalse(constant_time_compare('spam', 'eggs'))
class TestUtilsCryptoPBKDF2(unittest.TestCase): class TestUtilsCryptoPBKDF2(unittest.TestCase):