Module: AspisInit

Defined in:
lib/aspis/aspis_init.rb

Class Method Summary collapse

Class Method Details

.initObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/aspis/aspis_init.rb', line 24

def self.init
  options = {}
  OptionParser.new do |opts|
    opts.banner = "Encryption filter utility.\n"

    opts.version = Aspis::VERSION

    opts.on('-e', '--encrypt', 'Encrypt') do
      options[:mode] = 'encrypt'
    end

    opts.on('-d', '--decrypt', 'Decrypt') do
      options[:mode] = 'decrypt'
    end

    opts.on('-g', '--generate', 'Generate key pair') do
      options[:mode] = 'generate'
    end

    opts.on('-n', '--nopass', 'Use env variable ASPIS_PASS for passphrase') do
      options[:ask_pass] = false
    end

    opts.on('-p', '--pubkey public_key', String, "Recipient's public key") do |p|
      options[:public_key] = p
    end

    opts.on('-k', '--privatekey private_key', String, "Sender's private key") do |k|
      options[:private_key] = k
    end

    opts.on('-o', '--opslimit opslimit', Integer, 'Argon2i ops parameter') do |o|
      options[:opslimit] = o
    end

    opts.on('-m', '--memlimit memlimit', Integer, 'Argon2i memory in MiB') do |m|
      options[:memlimit] = m
    end

    opts.on('-h', '--help', 'Displays help') do
      puts opts
      exit
    end
  end.parse!

  abort('Fatal error: must enter -g, -e, or -d') unless options[:mode]

  run(options)
end

.run(options) ⇒ Object



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
# File 'lib/aspis/aspis_init.rb', line 74

def self.run(options)
  if options[:public_key]
    unless options[:private_key]
      aspis_dir = File.expand_path('~/.aspis')
      options[:private_key] = aspis_dir + '/private_key'
    end
  end

  case options[:mode]
  when 'encrypt'
    if options[:public_key]
      puts Asymmetric.encrypt(ARGF.read, options[:public_key], options[:private_key], options[:ask_pass])
    else
      puts Symmetric.encrypt(ARGF.read, options[:opslimit], options[:memlimit], options[:ask_pass])
    end
  when 'decrypt'
    if options[:public_key]
      puts Asymmetric.decrypt(ARGF.read, options[:public_key], options[:private_key], options[:ask_pass])
    else
      puts Symmetric.decrypt(ARGF.read, options[:ask_pass])
    end
  when 'generate'
    GenerateKeys.generate(options[:opslimit], options[:memlimit], options[:ask_pass])
  end
end