diff options
author | Vipul A M <vipulnsward@gmail.com> | 2016-07-09 15:51:56 -0700 |
---|---|---|
committer | Vipul A M <vipulnsward@gmail.com> | 2016-09-01 03:00:10 +0530 |
commit | 79c847892f5a08515769ad2598d4e6726dcd65e3 (patch) | |
tree | d330726952e6e5a8175a0144cf7de4d272703aea /activesupport | |
parent | ae32b69ab9647f4072d6852c4d4d1f2a939360c1 (diff) | |
download | rails-79c847892f5a08515769ad2598d4e6726dcd65e3.tar.gz rails-79c847892f5a08515769ad2598d4e6726dcd65e3.tar.bz2 rails-79c847892f5a08515769ad2598d4e6726dcd65e3.zip |
Start passing cipher from EncryptedCookieJar since we use it to determine key length
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/message_encryptor.rb | 7 | ||||
-rw-r--r-- | activesupport/test/message_encryptor_test.rb | 4 |
2 files changed, 9 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/message_encryptor.rb b/activesupport/lib/active_support/message_encryptor.rb index ab8b2f8ded..7b33dc3481 100644 --- a/activesupport/lib/active_support/message_encryptor.rb +++ b/activesupport/lib/active_support/message_encryptor.rb @@ -19,6 +19,8 @@ module ActiveSupport # encrypted_data = crypt.encrypt_and_sign('my secret data') # => "NlFBTTMwOUV5UlA1QlNEN2xkY2d6eThYWWh..." # crypt.decrypt_and_verify(encrypted_data) # => "my secret data" class MessageEncryptor + DEFAULT_CIPHER = "aes-256-cbc" + module NullSerializer #:nodoc: def self.load(value) value @@ -77,6 +79,11 @@ module ActiveSupport _decrypt(verifier.verify(value)) end + # Given a cipher, returns the key length of the cipher to help generate the key of desired size + def self.key_len(cipher = DEFAULT_CIPHER) + OpenSSL::Cipher.new(cipher).key_len + end + private def _encrypt(value) diff --git a/activesupport/test/message_encryptor_test.rb b/activesupport/test/message_encryptor_test.rb index fc17fdf3ee..f8282c89ca 100644 --- a/activesupport/test/message_encryptor_test.rb +++ b/activesupport/test/message_encryptor_test.rb @@ -50,12 +50,12 @@ class MessageEncryptorTest < ActiveSupport::TestCase def test_backwards_compat_for_64_bytes_key # 64 bit key - secret = ["3942b1bf81e622559ed509e3ff274a780784fe9e75b065866bd270438c74da822219de3156473cc27df1fd590e4baf68c95eeb537b6e4d4c5a10f41635b5597e"].pack('H*') + secret = ["3942b1bf81e622559ed509e3ff274a780784fe9e75b065866bd270438c74da822219de3156473cc27df1fd590e4baf68c95eeb537b6e4d4c5a10f41635b5597e"].pack("H*") # Encryptor with 32 bit key, 64 bit secret for verifier encryptor = ActiveSupport::MessageEncryptor.new(secret[0..31], secret) # Message generated with 64 bit key message = "eHdGeExnZEwvMSt3U3dKaFl1WFo0TjVvYzA0eGpjbm5WSkt5MXlsNzhpZ0ZnbWhBWFlQZTRwaXE1bVJCS2oxMDZhYVp2dVN3V0lNZUlWQ3c2eVhQbnhnVjFmeVVubmhRKzF3WnZyWHVNMDg9LS1HSisyakJVSFlPb05ISzRMaXRzcFdBPT0=--831a1d54a3cda8a0658dc668a03dedcbce13b5ca" - assert_equal 'data', encryptor.decrypt_and_verify(message)[:some] + assert_equal "data", encryptor.decrypt_and_verify(message)[:some] end def test_alternative_serialization_method |