4 ms·
For websites yes, but often you want to transmit the hashed value over the network. In that case it might be easier to implement the constant-time comparison.
by metachris 16y ago
For websites yes, but often you want to transmit the hashed value over the network. In that case it might be easier to implement the constant-time comparison.
Python:
def is_equal(a, b):
if len(a) != len(b):
return False
result = 0
for x, y in zip(a, b):
result |= x ^ y
return result == 0
Java:
public static boolean isEqual(byte[] a, byte[] b) {
if (a.length != b.length) {
return false;
}
int result = 0;
for (int i = 0; i < a.length; i++) {
result |= a[i] ^ b[i]
}
return result == 0;
}
Code from http://codahale.com/a-lesson-in-timing-attacks/ http://codahale.com/a-lesson-in-timing-attacks/
- tptacek 16y agoIt is a bad idea to send hashed passwords over a network. If you need a level of security beyond "passwords on the wire", use TLS.
- metachris 16y agoYes I fully agree. But sometimes you can't always use SSL/TLS (eg. for performance reasons within games). For certain requests you might want to add a hash for integrity protection and in that case absolutely use constant-time comparisons.
- deleted 16y ago[deleted]
- julian37 16y agoI might be missing something, but wouldn't that code allow to guess the password length because it returns more quickly if the length doesn't match? As far as I can see, the following would give away less information: def is_equal(actual, submitted): result = 0 for i in range(0, len(submitted)): result |= ord(actual[i % len(actual)]) ^ ord(submitted[i]) return result == 0 and len(actual) == len(submitted)