Class: Trustworthy::CLI::Decrypt

Inherits:
Object
  • Object
show all
Includes:
Command, Crypt
Defined in:
lib/trustworthy/cli/decrypt.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Crypt

#_check_options, #_command, included, #run

Methods included from Command

#default_options, #print_help, #say

Class Method Details

._commandObject



7
8
9
# File 'lib/trustworthy/cli/decrypt.rb', line 7

def self._command
  'decrypt filename'
end

Instance Method Details

#_strip_ciphertext(ciphertext) ⇒ Object



36
37
38
39
40
41
# File 'lib/trustworthy/cli/decrypt.rb', line 36

def _strip_ciphertext(ciphertext)
  ciphertext.
    gsub(/-+(BEGIN|END) TRUSTWORTHY ENCRYPTED FILE-+/, '').
    gsub(/^Version: .*$/, '').
    gsub("\n", '')
end

#_transform(prompt, options, input_file) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/trustworthy/cli/decrypt.rb', line 19

def _transform(prompt, options, input_file)
  wrapped_ciphertext = input_file.read
  unless wrapped_ciphertext.include?('TRUSTWORTHY ENCRYPTED FILE')
    say("File #{options[:input_file]} does not appear to be a trustworthy encrypted file")
    throw :error
  end

  master_key = prompt.unlock_master_key
  ciphertext = _strip_ciphertext(wrapped_ciphertext)
  plaintext = master_key.decrypt(ciphertext)
  File.open(options[:output_file], 'wb+') do |output_file|
    output_file.write(plaintext)
  end

  say("Decrypted #{options[:input_file]} to #{options[:output_file]}")
end

#parse_options(args) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/trustworthy/cli/decrypt.rb', line 11

def parse_options(args)
  options = super(args)
  if options[:input_file].to_s.end_with?('.tw') && !options.has_key?(:output_file)
    options[:output_file] = File.basename(options[:input_file], '.tw')
  end
  options
end