Skip to content

Commit

Permalink
Dup the arguments to string compare so we can use force_encoding.
Browse files Browse the repository at this point in the history
Conflicts:

	activesupport/lib/active_support/message_verifier.rb
  • Loading branch information
NZKoz committed Sep 12, 2009
1 parent 2524ac8 commit 6ddb7de
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions activesupport/lib/active_support/message_verifier.rb
Expand Up @@ -38,14 +38,17 @@ def generate(value)
end end


private private
if "foo".respond_to?(:bytesize) if "foo".respond_to?(:force_encoding)
# constant-time comparison algorithm to prevent timing attacks # constant-time comparison algorithm to prevent timing attacks
# > 1.8.6 friendly version
def secure_compare(a, b) def secure_compare(a, b)
if a.bytesize == b.bytesize a = a.dup.force_encoding(Encoding::BINARY)
b = b.dup.force_encoding(Encoding::BINARY)

if a.length == b.length
result = 0 result = 0
j = b.each_byte for i in 0..(a.length - 1)
a.each_byte { |i| result |= i ^ j.next } result |= a[i].ord ^ b[i].ord
end
result == 0 result == 0
else else
false false
Expand Down

0 comments on commit 6ddb7de

Please sign in to comment.