Created
May 2, 2011 02:03
-
-
Save levigross/951097 to your computer and use it in GitHub Desktop.
Safe hash comparison
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
from itertools import izip | |
def compare_hash(hashone,hashtwo): | |
if len(hashone) == len(hashtwo): # Every hash should be the same length | |
if sum((ord(o) ^ ord(t) for o,t in izip(hashone,hashtwo))): | |
return False | |
else: | |
return True | |
else: | |
return False |
I agree, I just wrote this in a verbose manner so people will understand the goal behind the code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any time you find yourself doing "if statement: return True else: return False" just return the statement. For example line 4 could be:
Or even more simply the whole thing is an and of two booleans: so