命名空間
- 類別 ActiveSupport::EncryptedFile::InvalidKeyLengthError
- 類別 ActiveSupport::EncryptedFile::MissingContentError
- 類別 ActiveSupport::EncryptedFile::MissingKeyError
方法
常數
CIPHER | = | "aes-128-gcm" |
屬性
[R] | content_path | |
[R] | env_key | |
[R] | key_path | |
[R] | raise_if_missing_key |
類別公開方法
generate_key() 連結
來源:顯示 | 在 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:) 連結
來源:顯示 | 在 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) 連結
來源:顯示 | 在 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`。
來源:顯示 | 在 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`。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/encrypted_file.rb, line 58 def key? read_env_key || read_key_file end
read() 連結
讀取檔案並回傳解密後的內容。
拋出例外
-
如果金鑰遺失且 `raise_if_missing_key` 為 true,則拋出 `MissingKeyError`。
-
如果加密檔案不存在或金鑰遺失,則拋出 `MissingContentError`。
-
如果內容無法解密或驗證,則拋出 `ActiveSupport::MessageEncryptor::InvalidMessage`。
來源:顯示 | 在 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) 連結
來源:顯示 | 在 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