Created
October 29, 2013 02:17
-
-
Save rennex/7208172 to your computer and use it in GitHub Desktop.
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
# constant-time comparison algorithm to prevent timing attacks | |
# (borrowed from devise) | |
def secure_compare(a, b) | |
return false if a.bytesize != b.bytesize | |
l = a.unpack "C#{a.bytesize}" | |
res = 0 | |
b.each_byte { |byte| res |= byte ^ l.shift } | |
res == 0 | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment