]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Python 3.3 includes a fast time-independent comparison function, so use
authorBen Darnell <ben@bendarnell.com>
Sat, 29 Sep 2012 18:27:46 +0000 (11:27 -0700)
committerBen Darnell <ben@bendarnell.com>
Sat, 29 Sep 2012 18:27:46 +0000 (11:27 -0700)
it when available.

tornado/web.py

index 21508e70ee675e85d3ffda84bd2a94274d4ff811..68bf78b1672d398b6164edff2fbe41fc185f9f45 100644 (file)
@@ -2047,17 +2047,20 @@ class URLSpec(object):
 url = URLSpec
 
 
-def _time_independent_equals(a, b):
-    if len(a) != len(b):
-        return False
-    result = 0
-    if type(a[0]) is int:  # python3 byte strings
-        for x, y in zip(a, b):
-            result |= x ^ y
-    else:  # python2
-        for x, y in zip(a, b):
-            result |= ord(x) ^ ord(y)
-    return result == 0
+if hasattr(hmac, 'compare_digest'):  # python 3.3
+    _time_independent_equals = hmac.compare_digest
+else:
+    def _time_independent_equals(a, b):
+        if len(a) != len(b):
+            return False
+        result = 0
+        if type(a[0]) is int:  # python3 byte strings
+            for x, y in zip(a, b):
+                result |= x ^ y
+        else:  # python2
+            for x, y in zip(a, b):
+                result |= ord(x) ^ ord(y)
+        return result == 0
 
 
 def create_signed_value(secret, name, value):