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-
要使用的压缩器。它必须响应
deflate和inflate。如果未提供,则默认为ActiveRecord::Encryption.config.compressor,后者本身默认为Zlib。
源码: 显示 | 在 GitHub 上
# 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?() 链接
源码: 显示 | 在 GitHub 上
# 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: {}) 链接
源码: 显示 | 在 GitHub 上
# 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 并返回加密结果。
内部,它将
-
将
clean_text压缩并加密为消息有效负载。 -
使用
ActiveRecord::Encryption.message_serializer(默认为ActiveRecord::Encryption::SafeMarshal)将其序列化。 -
使用 Base64 对结果进行编码。
选项¶ ↑
源码: 显示 | 在 GitHub 上
# 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) 链接
返回文本是否已加密。
源码: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/encryption/encryptor.rb, line 79 def encrypted?(text) deserialize_message(text) true rescue Errors::Encoding, *DECRYPT_ERRORS false end