aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support
diff options
context:
space:
mode:
authorWillem van Bergen <willem@vanbergen.org>2011-09-15 13:15:21 -0400
committerWillem van Bergen <willem@vanbergen.org>2011-09-15 13:15:21 -0400
commitdb040cdf8ba832123bae68764189bbcb569d473a (patch)
tree23da91cac4f604fe3829ac242af950a665390a96 /activesupport/lib/active_support
parenta8aaef676217f53f2812cd56f71a6b00c5d22162 (diff)
downloadrails-db040cdf8ba832123bae68764189bbcb569d473a.tar.gz
rails-db040cdf8ba832123bae68764189bbcb569d473a.tar.bz2
rails-db040cdf8ba832123bae68764189bbcb569d473a.zip
Implement API suggestions of pull request.
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r--activesupport/lib/active_support/message_encryptor.rb11
-rw-r--r--activesupport/lib/active_support/message_verifier.rb11
2 files changed, 10 insertions, 12 deletions
diff --git a/activesupport/lib/active_support/message_encryptor.rb b/activesupport/lib/active_support/message_encryptor.rb
index 05d6790075..66e121be26 100644
--- a/activesupport/lib/active_support/message_encryptor.rb
+++ b/activesupport/lib/active_support/message_encryptor.rb
@@ -13,13 +13,12 @@ module ActiveSupport
class InvalidMessage < StandardError; end
OpenSSLCipherError = OpenSSL::Cipher.const_defined?(:CipherError) ? OpenSSL::Cipher::CipherError : OpenSSL::CipherError
- attr_accessor :serializer, :deserializer
+ attr_accessor :serializer
- def initialize(secret, cipher = 'aes-256-cbc')
+ def initialize(secret, cipher = 'aes-256-cbc', serializer = Marshal)
@secret = secret
@cipher = cipher
- @serializer = lambda { |value| Marshal.dump(value) }
- @deserializer = lambda { |value| Marshal.load(value) }
+ @serializer = serializer
end
def encrypt(value)
@@ -31,7 +30,7 @@ module ActiveSupport
cipher.key = @secret
cipher.iv = iv
- encrypted_data = cipher.update(serializer.call(value))
+ encrypted_data = cipher.update(serializer.dump(value))
encrypted_data << cipher.final
[encrypted_data, iv].map {|v| ActiveSupport::Base64.encode64s(v)}.join("--")
@@ -48,7 +47,7 @@ module ActiveSupport
decrypted_data = cipher.update(encrypted_data)
decrypted_data << cipher.final
- deserializer.call(decrypted_data)
+ serializer.load(decrypted_data)
rescue OpenSSLCipherError, TypeError
raise InvalidMessage
end
diff --git a/activesupport/lib/active_support/message_verifier.rb b/activesupport/lib/active_support/message_verifier.rb
index e38e242cfe..b3a087a596 100644
--- a/activesupport/lib/active_support/message_verifier.rb
+++ b/activesupport/lib/active_support/message_verifier.rb
@@ -21,13 +21,12 @@ module ActiveSupport
class MessageVerifier
class InvalidSignature < StandardError; end
- attr_accessor :serializer, :deserializer
+ attr_accessor :serializer
- def initialize(secret, digest = 'SHA1')
+ def initialize(secret, digest = 'SHA1', serializer = Marshal)
@secret = secret
@digest = digest
- @serializer = lambda { |value| Marshal.dump(value) }
- @deserializer = lambda { |value| Marshal.load(value) }
+ @serializer = serializer
end
def verify(signed_message)
@@ -35,14 +34,14 @@ module ActiveSupport
data, digest = signed_message.split("--")
if data.present? && digest.present? && secure_compare(digest, generate_digest(data))
- deserializer.call(ActiveSupport::Base64.decode64(data))
+ serializer.load(ActiveSupport::Base64.decode64(data))
else
raise InvalidSignature
end
end
def generate(value)
- data = ActiveSupport::Base64.encode64s(serializer.call(value))
+ data = ActiveSupport::Base64.encode64s(serializer.dump(value))
"#{data}--#{generate_digest(data)}"
end