Class: ActiveRecord::Encryption::Encryptor
- Inherits:
-
Object
- Object
- ActiveRecord::Encryption::Encryptor
- Defined in:
- lib/active_record/encryption/encryptor.rb
Overview
An encryptor exposes the encryption API that ActiveRecord::Encryption::EncryptedAttributeType uses for encrypting and decrypting attribute values.
It interacts with a KeyProvider for getting the keys, and delegate to ActiveRecord::Encryption::Cipher the actual encryption algorithm.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#compressor ⇒ Object
readonly
The compressor to use for compressing the payload.
Instance Method Summary collapse
- #binary? ⇒ Boolean
-
#compress? ⇒ Boolean
:nodoc:.
-
#decrypt(encrypted_text, key_provider: default_key_provider, cipher_options: {}) ⇒ Object
Decrypts an
encrypted_text
and returns the result as clean text. -
#encrypt(clear_text, key_provider: default_key_provider, cipher_options: {}) ⇒ Object
Encrypts
clean_text
and returns the encrypted result. -
#encrypted?(text) ⇒ Boolean
Returns whether the text is encrypted or not.
-
#initialize(compress: true, compressor: nil) ⇒ Encryptor
constructor
Options.
Constructor Details
#initialize(compress: true, compressor: nil) ⇒ Encryptor
Options
:compress
-
Boolean indicating whether records should be compressed before encryption. Defaults to
true
. :compressor
-
The compressor to use. It must respond to
deflate
andinflate
. If not provided, will default toActiveRecord::Encryption.config.compressor
, which itself defaults toZlib
.
27 28 29 30 |
# File 'lib/active_record/encryption/encryptor.rb', line 27 def initialize(compress: true, compressor: nil) @compress = compress @compressor = compressor || ActiveRecord::Encryption.config.compressor end |
Instance Attribute Details
#compressor ⇒ Object (readonly)
The compressor to use for compressing the payload.
15 16 17 |
# File 'lib/active_record/encryption/encryptor.rb', line 15 def compressor @compressor end |
Instance Method Details
#binary? ⇒ Boolean
86 87 88 |
# File 'lib/active_record/encryption/encryptor.rb', line 86 def binary? serializer.binary? end |
#compress? ⇒ Boolean
:nodoc:
90 91 92 |
# File 'lib/active_record/encryption/encryptor.rb', line 90 def compress? # :nodoc: @compress end |
#decrypt(encrypted_text, key_provider: default_key_provider, cipher_options: {}) ⇒ Object
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_provider
when not provided. :cipher_options
-
Cipher-specific options that will be passed to the Cipher configured in
ActiveRecord::Encryption.cipher
.
69 70 71 72 73 74 75 76 |
# File 'lib/active_record/encryption/encryptor.rb', line 69 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: {}) ⇒ Object
Encrypts clean_text
and returns the encrypted result.
Internally, it will:
-
Create a new ActiveRecord::Encryption::Message.
-
Compress and encrypt
clean_text
as the message payload. -
Serialize it with
ActiveRecord::Encryption.message_serializer
(ActiveRecord::Encryption::SafeMarshal
by default). -
Encode the result with Base64.
Options
:key_provider
-
Key provider to use for the encryption operation. It will default to
ActiveRecord::Encryption.key_provider
when not provided. :cipher_options
-
Cipher-specific options that will be passed to the Cipher configured in
ActiveRecord::Encryption.cipher
.
51 52 53 54 55 56 |
# File 'lib/active_record/encryption/encryptor.rb', line 51 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 |