diff options
author | José Valim <jose.valim@gmail.com> | 2011-09-15 13:22:05 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-09-15 13:22:05 -0700 |
commit | 8a32a58f6f95deeae1ffe6f5dba9c4d71fbc8dff (patch) | |
tree | 9ac563affb6d71dc06943ddf296462afe41761d3 /activesupport/lib/active_support/message_encryptor.rb | |
parent | 28300f4ea86642eba5d60f5a64b9ea221f794031 (diff) | |
parent | 6d520803ee005d47c876662ba426083e823199ed (diff) | |
download | rails-8a32a58f6f95deeae1ffe6f5dba9c4d71fbc8dff.tar.gz rails-8a32a58f6f95deeae1ffe6f5dba9c4d71fbc8dff.tar.bz2 rails-8a32a58f6f95deeae1ffe6f5dba9c4d71fbc8dff.zip |
Merge pull request #3037 from wvanbergen/master
MessageVerifier: set serializer and crypto algorithm using an option hash
Diffstat (limited to 'activesupport/lib/active_support/message_encryptor.rb')
-rw-r--r-- | activesupport/lib/active_support/message_encryptor.rb | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/activesupport/lib/active_support/message_encryptor.rb b/activesupport/lib/active_support/message_encryptor.rb index 66e121be26..7637cfe2b0 100644 --- a/activesupport/lib/active_support/message_encryptor.rb +++ b/activesupport/lib/active_support/message_encryptor.rb @@ -13,12 +13,15 @@ module ActiveSupport class InvalidMessage < StandardError; end OpenSSLCipherError = OpenSSL::Cipher.const_defined?(:CipherError) ? OpenSSL::Cipher::CipherError : OpenSSL::CipherError - attr_accessor :serializer - - def initialize(secret, cipher = 'aes-256-cbc', serializer = Marshal) + def initialize(secret, options = {}) + unless options.is_a?(Hash) + ActiveSupport::Deprecation.warn "The second parameter should be an options hash. Use :cipher => 'algorithm' to sepcify the cipher algorithm." + options = { :cipher => options } + end + @secret = secret - @cipher = cipher - @serializer = serializer + @cipher = options[:cipher] || 'aes-256-cbc' + @serializer = options[:serializer] || Marshal end def encrypt(value) @@ -30,7 +33,7 @@ module ActiveSupport cipher.key = @secret cipher.iv = iv - encrypted_data = cipher.update(serializer.dump(value)) + encrypted_data = cipher.update(@serializer.dump(value)) encrypted_data << cipher.final [encrypted_data, iv].map {|v| ActiveSupport::Base64.encode64s(v)}.join("--") @@ -47,7 +50,7 @@ module ActiveSupport decrypted_data = cipher.update(encrypted_data) decrypted_data << cipher.final - serializer.load(decrypted_data) + @serializer.load(decrypted_data) rescue OpenSSLCipherError, TypeError raise InvalidMessage end |