跳至内容 跳至搜索

用于加密和解密Message对象的算法。

它使用AES-256-GCM。它将为非确定性加密(默认)生成一个随机IV,或从加密内容派生一个初始化向量以进行确定性加密。

参见Cipher::Aes256Gcm

命名空间
方法
D
E
I
K

常量

DEFAULT_ENCODING = Encoding::UTF_8
 

实例公有方法

解密(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

加密(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