class Exportation::Crypter

Attributes

files[RW]
output[RW]
password[RW]

Public Class Methods

new(options) click to toggle source
# File lib/exportation.rb, line 59
def initialize(options)
  @files = options[:files]
  @password = options[:password]
  @output = options[:output]
end

Public Instance Methods

run(crypt, force = false) click to toggle source
# File lib/exportation.rb, line 65
def run(crypt, force = false)
  run_commands(crypt, force).each do |bash|
    puts "Running: #{bash}"
    `#{bash}`
  end
end
run_commands(crypt, force = false) click to toggle source
# File lib/exportation.rb, line 72
def run_commands(crypt, force = false)
  raise "password is required" if Exportation.is_empty?(@password)

  unless force
    if crypt == :en
      # Verify files are not already encrypted
      files.each do |file|
        raise 'Some of these files may be encrypted (ending with .enc)' if file.end_with? '.enc'
      end
    elsif crypt == :de
      # Verify files are not already decrypted
      files.each do |file|
        raise 'Some of these files may be encrypted (ending with .enc)' unless file.end_with? '.enc'
      end
    end
  end

  # Does the stuff
  commands = []
  files.each do |file|
    file = './' + file unless file.start_with? '/'
    if File.exists? file
      output_file = file
      if !output.nil? && output.length > 0
        output_file = File.join(output, File.basename(file))
      end

      decrypt = ''
      if crypt == :en
        output_file += '.enc'
      elsif crypt == :de
        decrypt = ' -d'
        output_file = output_file.gsub('.enc','')
      end

      commands << "openssl aes-256-cbc -k \"#{password}\" -in #{file} -out #{output_file} -a#{decrypt}"
    else
      raise "File does not exist - #{file}"
    end
  end

  commands
end