Class: Trustworthy::CLI::Encrypt

Inherits:
Object
  • Object
show all
Includes:
Command, Crypt
Defined in:
lib/trustworthy/cli/encrypt.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/encrypt.rb', line 7

def self._command
  'encrypt filename'
end

Instance Method Details

#_format_ciphertext(output_file, ciphertext) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/trustworthy/cli/encrypt.rb', line 30

def _format_ciphertext(output_file, ciphertext)
  wrapped_ciphertext = ciphertext.scan(/.{1,64}/).join("\n")
  output_file.puts('-----BEGIN TRUSTWORTHY ENCRYPTED FILE-----')
  output_file.puts("Version: Trustworthy/#{Trustworthy::VERSION}")
  output_file.puts
  output_file.puts(wrapped_ciphertext)
  output_file.puts('-----END TRUSTWORTHY ENCRYPTED FILE-----')
end

#_transform(prompt, options, input_file) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/trustworthy/cli/encrypt.rb', line 19

def _transform(prompt, options, input_file)
  plaintext = input_file.read
  master_key = prompt.unlock_master_key
  ciphertext = master_key.encrypt(plaintext)
  File.open(options[:output_file], 'wb+') do |output_file|
    _format_ciphertext(output_file, ciphertext)
  end

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

#parse_options(args) ⇒ Object



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

def parse_options(args)
  options = super(args)
  unless options.key?(:output_file)
    options[:output_file] = "#{options[:input_file]}.tw"
  end
  options
end