在某些库中,例如flask-bcrypt,如果两个字符串的长度不同,我们可以看到代码提前退出:
def constant_time_compare(val1, val2):
'''Returns True if the two strings are equal, False otherwise.
The time taken is independent of the number of characters that match.
'''
if len(val1) != len(val2):
return False
result = 0
for x, y in zip(val1, val2):
result |= ord(x) ^ ord(y)
return result == 0
这真的安全吗?这肯定会向攻击者揭示这两个字符串早期的长度不同并泄露了信息?