Skip to content

Instantly share code, notes, and snippets.

@tomleo
Created January 6, 2015 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomleo/155ceceb41287e777e81 to your computer and use it in GitHub Desktop.
Save tomleo/155ceceb41287e777e81 to your computer and use it in GitHub Desktop.
Checking Equality of Hashes
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