命名空间
- CLASS ActiveSupport::EncryptedFile::InvalidKeyLengthError
- CLASS ActiveSupport::EncryptedFile::MissingContentError
- CLASS ActiveSupport::EncryptedFile::MissingKeyError
方法
常量
| CIPHER | = | "aes-128-gcm" |
Attributes
| [R] | content_path | |
| [R] | env_key | |
| [R] | key_path | |
| [R] | raise_if_missing_key |
类公共方法
generate_key() 链接
Source: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/encrypted_file.rb, line 31 def self.generate_key SecureRandom.hex(ActiveSupport::MessageEncryptor.key_len(CIPHER)) end
new(content_path:, key_path:, env_key:, raise_if_missing_key:) 链接
Source: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/encrypted_file.rb, line 42 def initialize(content_path:, key_path:, env_key:, raise_if_missing_key:) @content_path = Pathname.new(content_path).yield_self { |path| path.symlink? ? path.realpath : path } @key_path = Pathname.new(key_path) @env_key, @raise_if_missing_key = env_key, raise_if_missing_key end
实例公共方法
change(&block) 链接
Source: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/encrypted_file.rb, line 83 def change(&block) writing read, &block end
key() 链接
返回加密密钥,首先尝试 env_key 指定的环境变量,然后尝试 key_path 指定的密钥文件。如果 raise_if_missing_key 为 true,则当环境变量未设置且密钥文件不存在时,会引发 MissingKeyError。
Source: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/encrypted_file.rb, line 52 def key read_env_key || read_key_file || handle_missing_key end
key?() 链接
如果 key 为真,则返回真值。否则返回假值。与 key 不同,当 raise_if_missing_key 为 true 时,它不会引发 MissingKeyError。
Source: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/encrypted_file.rb, line 58 def key? read_env_key || read_key_file end
read() 链接
读取文件并返回解密后的内容。
引发
-
MissingKeyError,如果密钥丢失且raise_if_missing_key为 true。 -
MissingContentError,如果加密文件不存在或密钥丢失。 -
ActiveSupport::MessageEncryptor::InvalidMessage,如果内容无法解密或验证。
Source: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/encrypted_file.rb, line 70 def read if !key.nil? && content_path.exist? decrypt content_path.binread.strip else raise MissingContentError, content_path end end
write(contents) 链接
Source: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/encrypted_file.rb, line 78 def write(contents) IO.binwrite "#{content_path}.tmp", encrypt(contents) FileUtils.mv "#{content_path}.tmp", content_path end