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 |
屬性
[R] | compressor | 用於壓縮載荷的壓縮器 |
類別公開方法
new(compress: true, compressor: nil) 連結
選項
-
:compress
- 布林值,表示是否應在加密前壓縮記錄。預設為true
。 -
:compressor
- 要使用的壓縮器。-
如果提供壓縮器,則會使用它。
-
如果沒有,它將使用 ActiveRecord::Encryption.config.compressor,其預設值為
Zlib
。
如果您想使用自訂壓縮器,它必須回應
deflate
和inflate
。 -
來源: 顯示 | 在 GitHub 上
# File activerecord/lib/active_record/encryption/encryptor.rb, line 25 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 84 def binary? serializer.binary? end
decrypt(encrypted_text, key_provider: default_key_provider, cipher_options: {}) 連結
解密 encrypted_text
並以純文字傳回結果
選項
來源: 顯示 | 在 GitHub 上
# File activerecord/lib/active_record/encryption/encryptor.rb, line 67 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: {}) 連結
加密 clear_text
並傳回已加密的結果
在內部,它將
-
壓縮並加密
clean_text
作為訊息的載荷 -
使用
ActiveRecord::Encryption.message_serializer
(預設為ActiveRecord::Encryption::SafeMarshal
) 序列化它 -
使用
Base
64 編碼結果
選項
來源:顯示 | 在 GitHub 上
# File activerecord/lib/active_record/encryption/encryptor.rb, line 49 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) 連結
傳回 text 是否已加密
來源:顯示 | 在 GitHub 上
# File activerecord/lib/active_record/encryption/encryptor.rb, line 77 def encrypted?(text) deserialize_message(text) true rescue Errors::Encoding, *DECRYPT_ERRORS false end