Created
March 26, 2014 18:08
-
-
Save mpellegrini/9789595 to your computer and use it in GitHub Desktop.
A Lesson In Timing Attacks (see http://codahale.com/a-lesson-in-timing-attacks/)
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
// Better | |
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; | |
} | |
// worse | |
public static boolean isEqual(byte digesta[], byte digestb[]) { | |
if (digesta.length != digestb.length) | |
return false; | |
for (int i = 0; i < digesta.length; i++) { | |
if (digesta[i] != digestb[i]) { | |
return false; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment