Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
19 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -374,6 +374,17 @@ def self.configure_warden! #:nodoc: | |
def self.friendly_token | ||
ActiveSupport::SecureRandom.base64(15).tr('+/=', 'xyz') | ||
end | ||
|
||
# constant-time comparison algorithm to prevent timing attacks | ||
def self.secure_compare(a, b) | ||
return false unless a.present? && b.present? | ||
return false unless a.bytesize == b.bytesize | ||
l = a.unpack "C#{a.bytesize}" | ||
|
||
res = 0 | ||
b.each_byte { |byte| res |= byte ^ l.shift } | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
josevalim
Author
Contributor
|
||
res == 0 | ||
end | ||
end | ||
|
||
require 'warden' | ||
|
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
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
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
In what way is this "constant-time"? It loops over every byte, and it doesn't even return as soon as there is a mismatch.