From 896475c192c4aad833b980fee07785925f04dc47 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Wed, 9 Sep 2009 10:27:19 +1200 Subject: [PATCH] Revert "Ruby 1.9: fix MessageVerifier#secure_compare" This reverts commit 91f65b714b7018a74402ee02a000b19a090ad556. MessageVerifier was never in 2.2 --- activesupport/Rakefile | 3 +- .../lib/active_support/message_verifier.rb | 77 ------------------- 2 files changed, 1 insertion(+), 79 deletions(-) delete mode 100644 activesupport/lib/active_support/message_verifier.rb diff --git a/activesupport/Rakefile b/activesupport/Rakefile index f7fd52c7d8c93..ccbab525babf9 100644 --- a/activesupport/Rakefile +++ b/activesupport/Rakefile @@ -88,8 +88,7 @@ task :release => [ :package ] do end -$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/lib" -require 'active_support/values/time_zone' +require 'lib/active_support/values/time_zone' namespace :tzinfo do desc "Update bundled tzinfo gem. Only copies the subset of classes and definitions required to support Rails time zone features." diff --git a/activesupport/lib/active_support/message_verifier.rb b/activesupport/lib/active_support/message_verifier.rb deleted file mode 100644 index 8d14423d9164c..0000000000000 --- a/activesupport/lib/active_support/message_verifier.rb +++ /dev/null @@ -1,77 +0,0 @@ -module ActiveSupport - # MessageVerifier makes it easy to generate and verify messages which are signed - # to prevent tampering. - # - # This is useful for cases like remember-me tokens and auto-unsubscribe links where the - # session store isn't suitable or available. - # - # Remember Me: - # cookies[:remember_me] = @verifier.generate([@user.id, 2.weeks.from_now]) - # - # In the authentication filter: - # - # id, time = @verifier.verify(cookies[:remember_me]) - # if time < Time.now - # self.current_user = User.find(id) - # end - # - class MessageVerifier - class InvalidSignature < StandardError; end - - def initialize(secret, digest = 'SHA1') - @secret = secret - @digest = digest - end - - def verify(signed_message) - data, digest = signed_message.split("--") - if secure_compare(digest, generate_digest(data)) - Marshal.load(ActiveSupport::Base64.decode64(data)) - else - raise InvalidSignature - end - end - - def generate(value) - data = ActiveSupport::Base64.encode64s(Marshal.dump(value)) - "#{data}--#{generate_digest(data)}" - end - - private - if "foo".respond_to?(:force_encoding) - # constant-time comparison algorithm to prevent timing attacks - def secure_compare(a, b) - a = a.force_encoding(Encoding::BINARY) - b = b.force_encoding(Encoding::BINARY) - - if a.length == b.length - result = 0 - for i in 0..(a.length - 1) - result |= a[i].ord ^ b[i].ord - end - result == 0 - else - false - end - end - else - # For 1.8 - def secure_compare(a, b) - if a.length == b.length - result = 0 - for i in 0..(a.length - 1) - result |= a[i] ^ b[i] - end - result == 0 - else - false - end - end - end - - def generate_digest(data) - require 'openssl' unless defined?(OpenSSL) - OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new(@digest), @secret, data) - end - end -end