Class: Exportation::Crypter

Inherits:
Object
  • Object
show all
Defined in:
lib/exportation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Crypter

Returns a new instance of Crypter.



59
60
61
62
63
# File 'lib/exportation.rb', line 59

def initialize(options)
  @files = options[:files]
  @password = options[:password]
  @output = options[:output]
end

Instance Attribute Details

#filesObject

Returns the value of attribute files.



57
58
59
# File 'lib/exportation.rb', line 57

def files
  @files
end

#outputObject

Returns the value of attribute output.



57
58
59
# File 'lib/exportation.rb', line 57

def output
  @output
end

#passwordObject

Returns the value of attribute password.



57
58
59
# File 'lib/exportation.rb', line 57

def password
  @password
end

Instance Method Details

#run(crypt, force = false) ⇒ Object



65
66
67
68
69
70
# 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) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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