Class: ActiveRecord::Encryption::Cipher
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
|
|
Inherits: | Object |
Defined in: | activerecord/lib/active_record/encryption/cipher.rb, activerecord/lib/active_record/encryption.rb, activerecord/lib/active_record/encryption/cipher/aes256_gcm.rb |
Overview
Constant Summary
-
DEFAULT_ENCODING =
# File 'activerecord/lib/active_record/encryption/cipher.rb', line 12Encoding::UTF_8
Class Method Summary
::ActiveSupport::Autoload
- Extended
Instance Method Summary
-
#decrypt(encrypted_message, key:)
Decrypt the provided
Message
. -
#encrypt(clean_text, key:, deterministic: false)
Encrypts the provided text and return an encrypted
Message
. - #iv_length
- #key_length
- #cipher_for(secret, deterministic: false) private
- #try_to_decrypt_with_each(encrypted_text, keys:) private
Instance Method Details
#cipher_for(secret, deterministic: false) (private)
[ GitHub ]#decrypt(encrypted_message, key:)
Decrypt the provided Message
.
When key
is an ::Array
, it will try all the keys raising a Errors::Decryption
if none works.
# File 'activerecord/lib/active_record/encryption/cipher.rb', line 25
def decrypt(, key:) try_to_decrypt_with_each(, keys: Array(key)).tap do |decrypted_text| decrypted_text.force_encoding( .headers.encoding || DEFAULT_ENCODING) end end
#encrypt(clean_text, key:, deterministic: false)
Encrypts the provided text and return an encrypted Message
.
# File 'activerecord/lib/active_record/encryption/cipher.rb', line 15
def encrypt(clean_text, key:, deterministic: false) cipher_for(key, deterministic: deterministic).encrypt(clean_text).tap do || .headers.encoding = clean_text.encoding.name unless clean_text.encoding == DEFAULT_ENCODING end end
#iv_length
[ GitHub ]#key_length
[ GitHub ]# File 'activerecord/lib/active_record/encryption/cipher.rb', line 31
def key_length Aes256Gcm.key_length end
#try_to_decrypt_with_each(encrypted_text, keys:) (private)
[ GitHub ]# File 'activerecord/lib/active_record/encryption/cipher.rb', line 40
def try_to_decrypt_with_each(encrypted_text, keys:) keys.each.with_index do |key, index| return cipher_for(key).decrypt(encrypted_text) rescue ActiveRecord::Encryption::Errors::Decryption raise if index == keys.length - 1 end end