Module: Minisign::CLI

Defined in:
lib/minisign/cli.rb

Overview

The command line interface. This module is not intended for library usage and is subject to breaking changes.

Class Method Summary collapse

Class Method Details

.change_password(options) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/minisign/cli.rb', line 106

def self.change_password(options)
  options[:s] ||= "#{Dir.home}/.minisign/minisign.key"
  private_key = begin
    Minisign::PrivateKey.new(File.read(options[:s]))
  rescue Minisign::PasswordMissingError
    print 'Password: '
    Minisign::PrivateKey.new(File.read(options[:s]), prompt)
  end
  print 'New Password: '
  new_password = options[:W] ? nil : prompt
  private_key.change_password! new_password
  File.write(options[:s], private_key)
end

.generate(options) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/minisign/cli.rb', line 61

def self.generate(options)
  secret_key = options[:s] || "#{Dir.home}/.minisign/minisign.key"
  public_key = options[:p] || './minisign.pub'
  prevent_overwrite!(public_key) unless options[:f]
  prevent_overwrite!(secret_key) unless options[:f]

  if options[:W]
    keypair = Minisign::KeyPair.new
    File.write(secret_key, keypair.private_key)
    File.write(public_key, keypair.public_key)
  else
    print 'Password: '
    password = prompt
    print "\nPassword (one more time): "
    password_confirmation = prompt
    if password != password_confirmation
      puts "\nPasswords don't match"
      exit 1
    end
    print "\nDeriving a key from the password in order to encrypt the secret key..."
    keypair = Minisign::KeyPair.new(password)
    File.write(secret_key, keypair.private_key)
    print " done\n"
    puts "The secret key was saved as #{options[:s]} - Keep it secret!"
    File.write(public_key, keypair.public_key)
    puts "The public key was saved as #{options[:p]} - That one can be public."
    pubkey = keypair.public_key.to_s.split("\n").pop
    puts "minisign -Vm <file> -P #{pubkey}"
  end
end

.prevent_overwrite!(file) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/minisign/cli.rb', line 50

def self.prevent_overwrite!(file)
  return unless File.exist? file

  puts 'Key generation aborted:'
  puts "#{file} already exists."
  puts ''
  puts 'If you really want to overwrite the existing key pair, add the -f switch to'
  puts 'force this operation.'
  exit 1
end

.promptObject



46
47
48
# File 'lib/minisign/cli.rb', line 46

def self.prompt
  $stdin.tty? ? $stdin.noecho(&:gets).chomp : $stdin.gets.chomp
end

.recreate(options) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/minisign/cli.rb', line 92

def self.recreate(options)
  secret_key = options[:s] || "#{Dir.home}/.minisign/minisign.key"
  public_key = options[:p] || './minisign.pub'
  private_key_contents = File.read(secret_key)
  begin
    # try without a password first
    private_key = Minisign::PrivateKey.new(private_key_contents)
  rescue Minisign::PasswordMissingError
    print 'Password: '
    private_key = Minisign::PrivateKey.new(private_key_contents, prompt)
  end
  File.write(public_key, private_key.public_key)
end

.sign(options) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/minisign/cli.rb', line 120

def self.sign(options)
  # TODO: multiple files
  options[:x] ||= "#{options[:m]}.minisig"
  options[:s] ||= "#{Dir.home}/.minisign/minisign.key"
  private_key = begin
    Minisign::PrivateKey.new(File.read(options[:s]))
  rescue Minisign::PasswordMissingError
    print 'Password: '
    Minisign::PrivateKey.new(File.read(options[:s]), prompt)
  end
  signature = private_key.sign(options[:m], File.read(options[:m]), options[:t], options[:c])
  File.write(options[:x], signature)
end

.usageObject

Command line usage



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/minisign/cli.rb', line 16

def self.usage
  puts 'Usage:'
  puts 'minisign -G [-f] [-p pubkey_file] [-s seckey_file] [-W]'
  puts 'minisign -R [-s seckey_file] [-p pubkey_file]'
  puts 'minisign -C [-s seckey_file] [-W]'
  puts 'minisign -S [-l] [-x sig_file] [-s seckey_file] [-c untrusted_comment]'
  puts '            [-t trusted_comment] -m file [file ...]'
  puts 'minisign -V [-H] [-x sig_file] [-p pubkey_file | -P pubkey] [-o] [-q] -m file'
  puts ''
  puts '-G                generate a new key pair'
  puts '-R                recreate a public key file from a secret key file'
  puts '-C                change/remove the password of the secret key'
  puts '-S                sign files'
  puts '-V                verify that a signature is valid for a given file'
  puts '-m <file>         file to sign/verify'
  puts '-o                combined with -V, output the file content after verification'
  puts '-p <pubkey_file>  public key file (default: ./minisign.pub)'
  puts '-P <pubkey>       public key, as a base64 string'
  puts '-s <seckey_file>  secret key file (default: ~/.minisign/minisign.key)'
  puts '-W                do not encrypt/decrypt the secret key with a password'
  puts '-x <sigfile>      signature file (default: <file>.minisig)'
  puts '-c <comment>      add a one-line untrusted comment'
  puts '-t <comment>      add a one-line trusted comment'
  puts '-q                quiet mode, suppress output'
  puts '-Q                pretty quiet mode, only print the trusted comment'
  puts '-f                force. Combined with -G, overwrite a previous key pair'
  puts '-v                display version number'
  puts ''
end

.verify(options) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/minisign/cli.rb', line 134

def self.verify(options)
  options[:x] ||= "#{options[:m]}.minisig"
  options[:p] ||= './minisign.pub'
  options[:P] ||= File.read(options[:p])
  public_key = Minisign::PublicKey.new(options[:P])
  message = File.read(options[:m])
  signature = Minisign::Signature.new(File.read(options[:x]))
  begin
    verification = public_key.verify(signature, message)
  rescue StandardError
    puts 'Signature verification failed'
    exit 1
  end
  return if options[:q]
  return puts message if options[:o]

  puts options[:Q] ? signature.trusted_comment : verification
end