Skip to content

Instantly share code, notes, and snippets.

@lucassmagal
Created December 24, 2012 22:10
Show Gist options
  • Save lucassmagal/4370831 to your computer and use it in GitHub Desktop.
Save lucassmagal/4370831 to your computer and use it in GitHub Desktop.
constant-time algorithm for string comparing in Python. This is a refactoring of a snippet found in http://codahale.com/a-lesson-in-timing-attacks/ to work with strings.
def is_equal(a, b):
result = 0
for x, y in zip(a, b):
result |= ord(x) ^ ord(y)
return result == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment