class EncryptedYaml::CLI::Encrypt

Public Class Methods

new(options) click to toggle source
# File lib/encrypted_yaml/cli/encrypt.rb, line 6
def initialize(options)
  @keyfile = options[:keyfile]
  @ivfile = options[:ivfile]
  @filename = options[:filename]
end

Public Instance Methods

encrypt() click to toggle source
# File lib/encrypted_yaml/cli/encrypt.rb, line 12
def encrypt
  data = File.read(@filename)

  cipher = get_cipher
  cipher.update(data) + cipher.final
end

Private Instance Methods

get_cipher() click to toggle source
# File lib/encrypted_yaml/cli/encrypt.rb, line 21
def get_cipher
  cipher = OpenSSL::Cipher::AES256.new :CBC
  cipher.encrypt
  set_key cipher
  set_iv cipher

  cipher
end
set_iv(cipher) click to toggle source
# File lib/encrypted_yaml/cli/encrypt.rb, line 40
def set_iv(cipher)
  if File.exists? @ivfile
    cipher.iv = File.read(@ivfile)
    return
  end

  iv = cipher.random_iv
  File.open(@ivfile, 'wb') { |f| f.write iv }
end
set_key(cipher) click to toggle source
# File lib/encrypted_yaml/cli/encrypt.rb, line 30
def set_key(cipher)
  if File.exists? @keyfile
    cipher.key = File.read(@keyfile)
    return
  end

  key = cipher.random_key
  File.open(@keyfile, 'wb') { |f| f.write key }
end