跳至內容 跳至搜尋

用於加密和解密 Message 物件的演算法。

它使用 AES-256-GCM。它會為非確定性加密產生隨機 IV (預設) 或根據加密內容衍生初始化向量,以進行確定性加密。

請參閱 Cipher::Aes256Gcm

命名空間
方法
D
E
I
K

常數

DEFAULT_ENCODING = Encoding::UTF_8
 

實例公開方法

decrypt(encrypted_message, key:)

解密所提供的 Message

keyArray 時,它將嘗試使用所有金鑰,如果沒有任何金鑰有效,則會引發 ActiveRecord::Encryption::Errors::Decryption

# File activerecord/lib/active_record/encryption/cipher.rb, line 25
def decrypt(encrypted_message, key:)
  try_to_decrypt_with_each(encrypted_message, keys: Array(key)).tap do |decrypted_text|
    decrypted_text.force_encoding(encrypted_message.headers.encoding || DEFAULT_ENCODING)
  end
end

encrypt(clean_text, key:, deterministic: false)

加密所提供的文字並傳回加密的 Message

# File activerecord/lib/active_record/encryption/cipher.rb, line 15
def encrypt(clean_text, key:, deterministic: false)
  cipher_for(key, deterministic: deterministic).encrypt(clean_text).tap do |message|
    message.headers.encoding = clean_text.encoding.name unless clean_text.encoding == DEFAULT_ENCODING
  end
end

iv_length()

# File activerecord/lib/active_record/encryption/cipher.rb, line 35
def iv_length
  Aes256Gcm.iv_length
end

key_length()

# File activerecord/lib/active_record/encryption/cipher.rb, line 31
def key_length
  Aes256Gcm.key_length
end