Search code examples
pythonstringalgorithmstring-comparisonxor

Why compare two strings via calculating xor of their characters?


Some time ago I found this function (unfortunately, I don't remember from where it came from, most likely from some Python framework) that compares two strings and returns a bool value. It's quite simple to understand what's going on here. Finding xor between char returns 1 (True) if they do not match.

def  cmp_strings(str1, str2):
    return len(str1) == len(str2) and sum(ord(x)^ord(y) for x, y in zip(str1, str2)) == 0

But why is this function used? Isn't it the same as str1==str2?


Solution

  • It takes a similar amount of time to compare any strings that have the same length. It's used for security when the strings are sensitive. Usually it's used to compare password hashes.

    If == is used, Python stops comparing characters when the first one not matching is found. This is bad for hashes because it could reveal how close a hash was to matching. This would help an attacker to brute force a password.

    This is how hmac.compare_digest works.