Created
May 9, 2015 13:49
-
-
Save pjan/e69545b8922679be66c6 to your computer and use it in GitHub Desktop.
Comparison of strings in a time attack safe way
This file contains hidden or 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
def safeEquals(a: String, b: String) = { | |
val a_bytes = a.getBytes | |
val b_bytes = b.getBytes | |
if (a_bytes.length != b_bytes.length) { | |
false | |
} | |
else { | |
a_bytes.zip(b_bytes).foldLeft(0){ (acc, t) => acc + (t._1 ^ t._2) } == 0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment