mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Reorganize MessageEncryptor
1) According to OpenSSL's documentation, cipher.random_iv must be called after cipher.encrypt and already sets the generated IV on the cipher. 2) OpenSSL::CipherError was moved to OpenSSL::Cipher::CipherError in Ruby 1.8.7. Since Rails 4 requires at least Ruby 1.9.3, support for the old location can be dropped.
This commit is contained in:
parent
41a90dd459
commit
91a0a1156e
1 changed files with 4 additions and 5 deletions
|
@ -28,7 +28,7 @@ module ActiveSupport
|
||||||
end
|
end
|
||||||
|
|
||||||
class InvalidMessage < StandardError; end
|
class InvalidMessage < StandardError; end
|
||||||
OpenSSLCipherError = OpenSSL::Cipher.const_defined?(:CipherError) ? OpenSSL::Cipher::CipherError : OpenSSL::CipherError
|
OpenSSLCipherError = OpenSSL::Cipher::CipherError
|
||||||
|
|
||||||
# Initialize a new MessageEncryptor. +secret+ must be at least as long as
|
# Initialize a new MessageEncryptor. +secret+ must be at least as long as
|
||||||
# the cipher key size. For the default 'aes-256-cbc' cipher, this is 256
|
# the cipher key size. For the default 'aes-256-cbc' cipher, this is 256
|
||||||
|
@ -66,12 +66,11 @@ module ActiveSupport
|
||||||
|
|
||||||
def _encrypt(value)
|
def _encrypt(value)
|
||||||
cipher = new_cipher
|
cipher = new_cipher
|
||||||
# Rely on OpenSSL for the initialization vector
|
|
||||||
iv = cipher.random_iv
|
|
||||||
|
|
||||||
cipher.encrypt
|
cipher.encrypt
|
||||||
cipher.key = @secret
|
cipher.key = @secret
|
||||||
cipher.iv = iv
|
|
||||||
|
# Rely on OpenSSL for the initialization vector
|
||||||
|
iv = cipher.random_iv
|
||||||
|
|
||||||
encrypted_data = cipher.update(@serializer.dump(value))
|
encrypted_data = cipher.update(@serializer.dump(value))
|
||||||
encrypted_data << cipher.final
|
encrypted_data << cipher.final
|
||||||
|
|
Loading…
Reference in a new issue