aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/message_encryptor.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/message_encryptor.rb')
-rw-r--r--activesupport/lib/active_support/message_encryptor.rb8
1 files changed, 6 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/message_encryptor.rb b/activesupport/lib/active_support/message_encryptor.rb
index 4f7cd12d48..05d6790075 100644
--- a/activesupport/lib/active_support/message_encryptor.rb
+++ b/activesupport/lib/active_support/message_encryptor.rb
@@ -13,9 +13,13 @@ module ActiveSupport
class InvalidMessage < StandardError; end
OpenSSLCipherError = OpenSSL::Cipher.const_defined?(:CipherError) ? OpenSSL::Cipher::CipherError : OpenSSL::CipherError
+ attr_accessor :serializer, :deserializer
+
def initialize(secret, cipher = 'aes-256-cbc')
@secret = secret
@cipher = cipher
+ @serializer = lambda { |value| Marshal.dump(value) }
+ @deserializer = lambda { |value| Marshal.load(value) }
end
def encrypt(value)
@@ -27,7 +31,7 @@ module ActiveSupport
cipher.key = @secret
cipher.iv = iv
- encrypted_data = cipher.update(Marshal.dump(value))
+ encrypted_data = cipher.update(serializer.call(value))
encrypted_data << cipher.final
[encrypted_data, iv].map {|v| ActiveSupport::Base64.encode64s(v)}.join("--")
@@ -44,7 +48,7 @@ module ActiveSupport
decrypted_data = cipher.update(encrypted_data)
decrypted_data << cipher.final
- Marshal.load(decrypted_data)
+ deserializer.call(decrypted_data)
rescue OpenSSLCipherError, TypeError
raise InvalidMessage
end