Class: ReversibleCryptography::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/reversible_cryptography/cli.rb

Instance Method Summary collapse

Instance Method Details

#decrypt(encrypted_text = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/reversible_cryptography/cli.rb', line 30

def decrypt(encrypted_text=nil)
  encrypted_text = File.read(options[:src_file]) if options[:src_file]
  encrypted_text ||= ask("Input text:")
  password = options[:password]
  password ||= ask("Input password:", echo: false).tap { puts }

  plain_text = ReversibleCryptography::Message.decrypt(encrypted_text, password)
  if options[:dst_file]
    File.open(options[:dst_file], "wb") do |f|
      f.write(plain_text)
    end
  else
    puts plain_text
  end
end

#edit(encrypted_file) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/reversible_cryptography/cli.rb', line 49

def edit(encrypted_file)
  password = options[:password]
  password ||= ask("Input password:", echo: false).tap { puts }
  editor_command = options[:editor] || ENV['EDITOR']

  Tempfile.open('decrypted_text') do |fp|
    decrypted_text = ReversibleCryptography::Message.decrypt(File.read(encrypted_file), password)
    fp.write(decrypted_text)
    fp.close

    system("#{editor_command} #{fp.path}")
    encrypted_text = ReversibleCryptography::Message.encrypt(File.read(fp.path), password)

    File.open(encrypted_file, "wb") do |f|
      f.write(encrypted_text)
    end
  end
end

#encrypt(plain_text = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/reversible_cryptography/cli.rb', line 10

def encrypt(plain_text=nil)
  plain_text = File.read(options[:src_file]) if options[:src_file]
  plain_text ||= ask("Input text:")
  password = options[:password]
  password ||= ask("Input password:", echo: false).tap { puts }

  encrypted_text = ReversibleCryptography::Message.encrypt(plain_text, password)
  if options[:dst_file]
    File.open(options[:dst_file], "wb") do |f|
      f.write(encrypted_text)
    end
  else
    puts encrypted_text
  end
end