Created
November 17, 2015 08:25
-
-
Save sled/495055011db3453051b5 to your computer and use it in GitHub Desktop.
Constant time comparisons
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 | |
def self.secure_compare(a, b) | |
return false if a.blank? || b.blank? || a.bytesize != b.bytesize | |
l = a.unpack "C#{a.bytesize}" | |
res = 0 | |
b.each_byte { |byte| res |= byte ^ l.shift } | |
res == 0 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment