Class: ActiveRecord::Encryption::Encryptor
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Subclasses:
|
|
| Inherits: | Object |
| Defined in: | activerecord/lib/active_record/encryption/encryptor.rb |
Overview
An encryptor exposes the encryption API that EncryptedAttributeType uses for encrypting and decrypting attribute values.
It interacts with a KeyProvider for getting the keys, and delegate to Cipher the actual encryption algorithm.
Constant Summary
-
DECRYPT_ERRORS =
# File 'activerecord/lib/active_record/encryption/encryptor.rb', line 83[OpenSSL::Cipher::CipherError, Errors::EncryptedContentIntegrity, Errors::Decryption]
-
ENCODING_ERRORS =
# File 'activerecord/lib/active_record/encryption/encryptor.rb', line 84[EncodingError, Errors::Encoding]
-
THRESHOLD_TO_JUSTIFY_COMPRESSION =
# File 'activerecord/lib/active_record/encryption/encryptor.rb', line 85140.bytes
Class Method Summary
-
.new(compress: true) ⇒ Encryptor
constructor
Options.
Instance Attribute Summary
- #binary? ⇒ Boolean readonly
Instance Method Summary
-
#decrypt(encrypted_text, key_provider: default_key_provider, cipher_options: {})
Decrypts an
encrypted_textand returns the result as clean text. -
#encrypt(clear_text, key_provider: default_key_provider, cipher_options: {})
Encrypts
clean_textand returns the encrypted result. -
#encrypted?(text) ⇒ Boolean
Returns whether the text is encrypted or not.
Constructor Details
.new(compress: true) ⇒ Encryptor
Options
:compress-
Boolean indicating whether records should be compressed before encryption. Defaults to
true.
# File 'activerecord/lib/active_record/encryption/encryptor.rb', line 20
def initialize(compress: true) @compress = compress end
Instance Attribute Details
#binary? ⇒ Boolean (readonly)
[ GitHub ]
# File 'activerecord/lib/active_record/encryption/encryptor.rb', line 78
def binary? serializer.binary? end
Instance Method Details
#decrypt(encrypted_text, key_provider: default_key_provider, cipher_options: {})
Decrypts an encrypted_text and returns the result as clean text.
Options
:key_provider-
Key provider to use for the encryption operation. It will default to
ActiveRecord::Encryption.key_providerwhen not provided. :cipher_options-
Cipher-specific options that will be passed to the Cipher configured in
ActiveRecord::Encryption.cipher.
# File 'activerecord/lib/active_record/encryption/encryptor.rb', line 61
def decrypt(encrypted_text, key_provider: default_key_provider, cipher_options: {}) = (encrypted_text) keys = key_provider.decryption_keys() raise Errors::Decryption unless keys.present? uncompress_if_needed(cipher.decrypt(, key: keys.collect(&:secret), **), .headers.compressed) rescue *(ENCODING_ERRORS + DECRYPT_ERRORS) raise Errors::Decryption end
#encrypt(clear_text, key_provider: default_key_provider, cipher_options: {})
Encrypts clean_text and returns the encrypted result.
Internally, it will:
-
Create a new
Message. -
Compress and encrypt
clean_textas the message payload. -
Serialize it with
ActiveRecord::Encryption.message_serializer(ActiveRecord::Encryption::SafeMarshalby default). -
Encode the result with Base64.
Options
:key_provider-
Key provider to use for the encryption operation. It will default to
ActiveRecord::Encryption.key_providerwhen not provided. :cipher_options-
Cipher-specific options that will be passed to the Cipher configured in
ActiveRecord::Encryption.cipher.
# File 'activerecord/lib/active_record/encryption/encryptor.rb', line 43
def encrypt(clear_text, key_provider: default_key_provider, cipher_options: {}) clear_text = force_encoding_if_needed(clear_text) if [:deterministic] validate_payload_type(clear_text) (clear_text, key_provider: key_provider, cipher_options: ) end
#encrypted?(text) ⇒ Boolean
Returns whether the text is encrypted or not.
# File 'activerecord/lib/active_record/encryption/encryptor.rb', line 71
def encrypted?(text) (text) true rescue Errors::Encoding, *DECRYPT_ERRORS false end