class ActiveSupport::EncryptedFile

Constants

CIPHER

Attributes

content_path[R]
env_key[R]
key_path[R]
raise_if_missing_key[R]

Public Class Methods

generate_key() click to toggle source
# File lib/active_support/encrypted_file.rb, line 23
def self.generate_key
  SecureRandom.hex(ActiveSupport::MessageEncryptor.key_len(CIPHER))
end
new(content_path:, key_path:, env_key:, raise_if_missing_key:) click to toggle source
# File lib/active_support/encrypted_file.rb, line 30
def initialize(content_path:, key_path:, env_key:, raise_if_missing_key:)
  @content_path, @key_path = Pathname.new(content_path), Pathname.new(key_path)
  @env_key, @raise_if_missing_key = env_key, raise_if_missing_key
end

Public Instance Methods

change(&block) click to toggle source
# File lib/active_support/encrypted_file.rb, line 52
def change(&block)
  writing read, &block
end
key() click to toggle source
# File lib/active_support/encrypted_file.rb, line 35
def key
  read_env_key || read_key_file || handle_missing_key
end
read() click to toggle source
# File lib/active_support/encrypted_file.rb, line 39
def read
  if !key.nil? && content_path.exist?
    decrypt content_path.binread
  else
    raise MissingContentError, content_path
  end
end
write(contents) click to toggle source
# File lib/active_support/encrypted_file.rb, line 47
def write(contents)
  IO.binwrite "#{content_path}.tmp", encrypt(contents)
  FileUtils.mv "#{content_path}.tmp", content_path
end