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
- .change_password(options) ⇒ Object
- .generate(options) ⇒ Object
- .prevent_overwrite!(file) ⇒ Object
- .private_key(seckey_file) ⇒ Object
- .prompt ⇒ Object
- .recreate(options) ⇒ Object
- .sign(options) ⇒ Object
-
.usage ⇒ Object
Command line usage.
- .verify(options) ⇒ Object
Class Method Details
.change_password(options) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/minisign/cli.rb', line 99 def self.change_password() [:s] ||= "#{Dir.home}/.minisign/minisign.key" new_private_key = private_key([:s]) print 'New Password: ' new_password = [:W] ? nil : prompt new_private_key.change_password! new_password File.write([:s], new_private_key) end |
.generate(options) ⇒ Object
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 91 |
# File 'lib/minisign/cli.rb', line 62 def self.generate() secret_key = [:s] || "#{Dir.home}/.minisign/minisign.key" public_key = [:p] || './minisign.pub' prevent_overwrite!(public_key) unless [:f] prevent_overwrite!(secret_key) unless [:f] if [: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 #{[:s]} - Keep it secret!" File.write(public_key, keypair.public_key) puts "The public key was saved as #{[: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
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/minisign/cli.rb', line 51 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 |
.private_key(seckey_file) ⇒ Object
135 136 137 138 139 140 141 142 143 |
# File 'lib/minisign/cli.rb', line 135 def self.private_key(seckey_file) seckey_file_contents = File.read(seckey_file) begin Minisign::PrivateKey.new(seckey_file_contents) rescue Minisign::PasswordMissingError print 'Password: ' Minisign::PrivateKey.new(seckey_file_contents, prompt) end end |
.prompt ⇒ Object
47 48 49 |
# File 'lib/minisign/cli.rb', line 47 def self.prompt $stdin.tty? ? $stdin.noecho(&:gets).chomp : $stdin.gets.chomp end |
.recreate(options) ⇒ Object
93 94 95 96 97 |
# File 'lib/minisign/cli.rb', line 93 def self.recreate() [:s] ||= "#{Dir.home}/.minisign/minisign.key" public_key = [:p] || './minisign.pub' File.write(public_key, private_key([:s]).public_key) end |
.sign(options) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/minisign/cli.rb', line 108 def self.sign() # TODO: multiple files [:x] ||= "#{[:m]}.minisig" [:s] ||= "#{Dir.home}/.minisign/minisign.key" signature = private_key([:s]).sign([:m], File.read([:m]), [:t], [:c]) File.write([:x], signature) end |
.usage ⇒ Object
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 45 |
# 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 '' exit 1 end |
.verify(options) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/minisign/cli.rb', line 116 def self.verify() [:x] ||= "#{[:m]}.minisig" [:p] ||= './minisign.pub' [:P] ||= File.read([:p]) public_key = Minisign::PublicKey.new([:P]) = File.read([:m]) signature = Minisign::Signature.new(File.read([:x])) begin verification = public_key.verify(signature, ) rescue Minisign::SignatureVerificationError => e puts e. exit 1 end return if [:q] return puts if [:o] puts [:Q] ? signature.trusted_comment : verification end |