instance method
encrypts
Ruby on Rails 7.0.10
Since v7.0.10Signature
encrypts(*names, key_provider: nil, key: nil, deterministic: false, downcase: false, ignore_case: false, previous: [], **context_properties)
Encrypts the name attribute.
Options
-
:key_provider- A key provider to provide encryption and decryption keys. Defaults toActiveRecord::Encryption.key_provider. -
:key- A password to derive the key from. It’s a shorthand for a:key_providerthat serves derivated keys. Both options can’t be used at the same time. -
:deterministic- By default, encryption is not deterministic. It will use a random initialization vector for each encryption operation. This means that encrypting the same content with the same key twice will generate different ciphertexts. When set totrue, it will generate the initialization vector based on the encrypted content. This means that the same content will generate the same ciphertexts. This enables querying encrypted text with Active Record. Deterministic encryption will use the oldest encryption scheme to encrypt new data by default. You can change this by setting +deterministic: { fixed: false }+. That will make it use the newest encryption scheme for encrypting new data. -
:downcase- When true, it converts the encrypted content to downcase automatically. This allows to effectively ignore case when querying data. Notice that the case is lost. Use:ignore_caseif you are interested in preserving it. -
:ignore_case- When true, it behaves like:downcasebut, it also preserves the original case in a specially designated column +original_<name>+. When reading the encrypted content, the version with the original case is served. But you can still execute queries that will ignore the case. This option can only be used when:deterministicis true. -
:context_properties- Additional properties that will overrideContextsettings when this attribute is encrypted and decrypted. E.g:encryptor:,cipher:,message_serializer:, etc. -
:previous- List of previous encryption schemes. When provided, they will be used in order when trying to read the attribute. Each entry of the list can contain the properties supported by #encrypts. Also, when deterministic encryption is used, they will be used to generate additional ciphertexts to check in the queries.
Parameters
-
namesrest -
key_providerkey = nil -
keykey = nil -
deterministickey = false -
downcasekey = false -
ignore_casekey = false -
previouskey = [] -
context_propertieskeyrest
Source
# File activerecord/lib/active_record/encryption/encryptable_record.rb, line 45
def encrypts(*names, key_provider: nil, key: nil, deterministic: false, downcase: false, ignore_case: false, previous: [], **context_properties)
self.encrypted_attributes ||= Set.new # not using :default because the instance would be shared across classes
scheme = scheme_for key_provider: key_provider, key: key, deterministic: deterministic, downcase: downcase, \
ignore_case: ignore_case, previous: previous, **context_properties
names.each do |name|
encrypt_attribute name, scheme
end
end
Defined in activerecord/lib/active_record/encryption/encryptable_record.rb line 45
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Encryption::EncryptableRecord