跳至内容 跳至搜索

Encryptor 暴露了 ActiveRecord::Encryption::EncryptedAttributeType 用于加密和解密属性值的加密 API。

它与 KeyProvider 交互以获取密钥,并将实际加密算法委托给 ActiveRecord::Encryption::Cipher

方法
B
D
E
N

常量

DECRYPT_ERRORS = [OpenSSL::Cipher::CipherError, Errors::EncryptedContentIntegrity, Errors::Decryption]
 
ENCODING_ERRORS = [EncodingError, Errors::Encoding]
 
THRESHOLD_TO_JUSTIFY_COMPRESSION = 140.bytes
 

此阈值不可更改。

用户可以搜索使用 ‘deterministic: true` 加密的属性。之所以可能,是因为我们能够确定性地为给定的明文生成消息,并通过该消息在 SQL 中执行常规字符串查找。

问题是,消息可能有一个“c”头,该头根据加密时是否应用了压缩而存在。如果修改了此阈值,为查找生成的消息对于相同的明文可能会有所不同,并且对现有数据的搜索可能会失败。

Attributes

[R] compressor

用于压缩有效负载的压缩器。

类公共方法

new(compress: true, compressor: nil)

选项

:compress

布尔值,指示记录是否应在加密前进行压缩。默认为 true

:compressor

要使用的压缩器。它必须响应 deflateinflate。如果未提供,则默认为 ActiveRecord::Encryption.config.compressor,后者本身默认为 Zlib

# File activerecord/lib/active_record/encryption/encryptor.rb, line 27
def initialize(compress: true, compressor: nil)
  @compress = compress
  @compressor = compressor || ActiveRecord::Encryption.config.compressor
end

实例公共方法

binary?()

# File activerecord/lib/active_record/encryption/encryptor.rb, line 86
def binary?
  serializer.binary?
end

decrypt(encrypted_text, key_provider: default_key_provider, cipher_options: {})

解密 encrypted_text 并将结果作为纯文本返回。

选项

:key_provider

用于加密操作的 Key 提供程序。如果不提供,它将默认为 ActiveRecord::Encryption.key_provider

:cipher_options

将传递给 ActiveRecord::Encryption.cipher 中配置的 Cipher 的特定于密码的选项。

# File activerecord/lib/active_record/encryption/encryptor.rb, line 69
def decrypt(encrypted_text, key_provider: default_key_provider, cipher_options: {})
  message = deserialize_message(encrypted_text)
  keys = key_provider.decryption_keys(message)
  raise Errors::Decryption unless keys.present?
  uncompress_if_needed(cipher.decrypt(message, key: keys.collect(&:secret), **cipher_options), message.headers.compressed)
rescue *(ENCODING_ERRORS + DECRYPT_ERRORS)
  raise Errors::Decryption
end

encrypt(clear_text, key_provider: default_key_provider, cipher_options: {})

加密 clean_text 并返回加密结果。

内部,它将

  1. 创建一个新的 ActiveRecord::Encryption::Message

  2. clean_text 压缩并加密为消息有效负载。

  3. 使用 ActiveRecord::Encryption.message_serializer(默认为 ActiveRecord::Encryption::SafeMarshal)将其序列化。

  4. 使用 Base64 对结果进行编码。

选项

:key_provider

用于加密操作的 Key 提供程序。如果不提供,它将默认为 ActiveRecord::Encryption.key_provider

:cipher_options

将传递给 ActiveRecord::Encryption.cipher 中配置的 Cipher 的特定于密码的选项。

# File activerecord/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 cipher_options[:deterministic]

  validate_payload_type(clear_text)
  serialize_message build_encrypted_message(clear_text, key_provider: key_provider, cipher_options: cipher_options)
end

encrypted?(text)

返回文本是否已加密。

# File activerecord/lib/active_record/encryption/encryptor.rb, line 79
def encrypted?(text)
  deserialize_message(text)
  true
rescue Errors::Encoding, *DECRYPT_ERRORS
  false
end