module EYAML

Constants

DEFAULT_KEYDIR
INTERNAL_PUB_KEY
SUPPORTED_EXTENSIONS
VERSION

Public Class Methods

decrypt(cipherdata, **key_options) click to toggle source
# File lib/eyaml.rb, line 46
def decrypt(cipherdata, **key_options)
  public_key = load_public_key(cipherdata)
  private_key = load_private_key_from(public_key: public_key, **key_options)

  encryption_manager = EncryptionManager.new(cipherdata, public_key, private_key)
  encryption_manager.decrypt
end
decrypt_file(file_path, **key_options) click to toggle source
# File lib/eyaml.rb, line 54
def decrypt_file(file_path, **key_options)
  cipherdata = YAML.load_file(file_path)
  plaindata = decrypt(cipherdata, **key_options)
  format_for_file(plaindata, file_path)
end
encrypt(plaindata) click to toggle source
# File lib/eyaml.rb, line 29
def encrypt(plaindata)
  public_key = load_public_key(plaindata)

  encryption_manager = EncryptionManager.new(plaindata, public_key)
  encryption_manager.encrypt
end
encrypt_file_in_place(file_path) click to toggle source
# File lib/eyaml.rb, line 36
def encrypt_file_in_place(file_path)
  plaindata = YAML.load_file(file_path)
  cipherdata = encrypt(plaindata)

  eyaml = format_for_file(cipherdata, file_path)

  File.write(file_path, eyaml)
  eyaml.bytesize
end
generate_keypair(save: false, keydir: nil) click to toggle source
# File lib/eyaml.rb, line 18
def generate_keypair(save: false, keydir: nil)
  public_key, private_key = EncryptionManager.new_keypair

  if save
    keypair_file_path = File.expand_path(public_key, ensure_keydir(keydir))
    File.write(keypair_file_path, private_key)
  end

  [public_key, private_key]
end

Private Class Methods

ensure_keydir(keydir) click to toggle source
# File lib/eyaml.rb, line 72
def ensure_keydir(keydir)
  keydir || ENV["EJSON_KEYDIR"] || DEFAULT_KEYDIR
end
format_for_file(data, file_path) click to toggle source
# File lib/eyaml.rb, line 76
def format_for_file(data, file_path)
  case File.extname(file_path)
  when ".eyaml", ".eyml"
    EYAML::Util.pretty_yaml(data)
  when ".ejson"
    JSON.pretty_generate(data)
  else
    raise EYAML::InvalidFormatError, "Unsupported file type"
  end
end
load_private_key_from(public_key:, keydir: nil, private_key: nil) click to toggle source
# File lib/eyaml.rb, line 67
def load_private_key_from(public_key:, keydir: nil, private_key: nil)
  return private_key unless private_key.nil?
  File.read(File.expand_path(public_key, ensure_keydir(keydir)))
end
load_public_key(data) click to toggle source
# File lib/eyaml.rb, line 62
def load_public_key(data)
  raise EYAML::MissingPublicKey unless data.has_key?(INTERNAL_PUB_KEY)
  data.fetch(INTERNAL_PUB_KEY)
end