Module: EYAML

Defined in:
lib/eyaml.rb,
lib/eyaml/cli.rb,
lib/eyaml/util.rb,
lib/eyaml/railtie.rb,
lib/eyaml/version.rb,
lib/eyaml/encryption_manager.rb

Defined Under Namespace

Modules: Rails Classes: CLI, EncryptionManager, InvalidFormatError, MissingPublicKey, UnsupportedVersionError, Util

Constant Summary collapse

DEFAULT_KEYDIR =
"/opt/ejson/keys"
INTERNAL_PUB_KEY =
"_public_key"
SUPPORTED_EXTENSIONS =
%w[eyaml eyml ejson]
VERSION =
"0.4.2"

Class Method Summary collapse

Class Method Details

.decrypt(cipherdata, **key_options) ⇒ Object



46
47
48
49
50
51
52
# 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) ⇒ Object



54
55
56
57
58
# 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) ⇒ Object



29
30
31
32
33
34
# 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) ⇒ Object



36
37
38
39
40
41
42
43
44
# 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) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# 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