Permalink
Browse files

Use AS secure_compare in AS::MessageVerifier

  • Loading branch information...
guilleiguaran committed Oct 23, 2014
1 parent c8c6600 commit 4b11dea3917f2101d7125a967877abf19c36317a
Showing with 2 additions and 12 deletions.
  1. +2 −12 activesupport/lib/active_support/message_verifier.rb
@@ -1,5 +1,6 @@
require 'base64'
require 'active_support/core_ext/object/blank'
require 'active_support/security_utils'
module ActiveSupport
# +MessageVerifier+ makes it easy to generate and verify messages which are
@@ -37,7 +38,7 @@ def verify(signed_message)
raise InvalidSignature if signed_message.blank?
data, digest = signed_message.split("--")
if data.present? && digest.present? && secure_compare(digest, generate_digest(data))
if data.present? && digest.present? && ActiveSupport::SecurityUtils.secure_compare(digest, generate_digest(data))
begin
@serializer.load(::Base64.strict_decode64(data))
rescue ArgumentError => argument_error
@@ -55,17 +56,6 @@ def generate(value)
end
private
# constant-time comparison algorithm to prevent timing attacks
def secure_compare(a, b)
return false unless a.bytesize == b.bytesize
l = a.unpack "C#{a.bytesize}"
res = 0
b.each_byte { |byte| res |= byte ^ l.shift }
res == 0
end
def generate_digest(data)
require 'openssl' unless defined?(OpenSSL)
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.const_get(@digest).new, @secret, data)

0 comments on commit 4b11dea

Please sign in to comment.