Created
January 6, 2015 16:36
-
-
Save tomleo/155ceceb41287e777e81 to your computer and use it in GitHub Desktop.
Checking Equality of Hashes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_equal(val1, val2): | |
""" | |
This is a really nice way to comparing two hash values using the set operation XOR | |
This snippet is from the plone project https://github.com/plone/plone.session/blob/master/plone/session/tktauth.py | |
""" | |
# constant time comparison | |
if not isinstance(val1, basestring) or not isinstance(val2, basestring): | |
return False | |
if len(val1) != len(val2): | |
return False | |
result = 0 | |
for x, y in zip(val1, val2): | |
result |= ord(x) ^ ord(y) # XOR i.e. returns the set of all of the values not shared between x and y | |
return result == 0 # the result set should be 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment