Class: OpenPGP::Client::GnuPG

Inherits:
Object
  • Object
show all
Defined in:
lib/openpgp/client/gnupg.rb

Overview

GNU Privacy Guard (GnuPG) implementation.

Constant Summary collapse

VERSION =
OpenPGP::VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GnuPG

Returns a new instance of GnuPG.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/openpgp/client/gnupg.rb', line 11

def initialize(options = {})
  @options = {
    :homedir => ENV['GNUPGHOME'] || '~/.gnupg',
    :version => false,
  }
  @options.merge!(options)

  if options.has_key?(:options)
    parse_options_file(options[:options])
  end
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/openpgp/client/gnupg.rb', line 9

def options
  @options
end

Instance Method Details

#card_editObject

Presents a menu to work with a smartcard.

Raises:

  • (NotImplementedError)


216
217
218
# File 'lib/openpgp/client/gnupg.rb', line 216

def card_edit
  raise NotImplementedError # TODO
end

#card_statusObject

Shows the content of the smart card.

Raises:

  • (NotImplementedError)


222
223
224
# File 'lib/openpgp/client/gnupg.rb', line 222

def card_status
  raise NotImplementedError # TODO
end

#change_pinObject

Presents a menu to allow changing the PIN of a smartcard.

Raises:

  • (NotImplementedError)


228
229
230
# File 'lib/openpgp/client/gnupg.rb', line 228

def change_pin
  raise NotImplementedError # TODO
end

#check_sigsObject

Same as list_sigs, but the signatures are verified.

Raises:

  • (NotImplementedError)


197
198
199
# File 'lib/openpgp/client/gnupg.rb', line 197

def check_sigs
  raise NotImplementedError # TODO
end

#check_trustdbObject

Does trust database maintenance without user interaction.

Raises:

  • (NotImplementedError)


319
320
321
# File 'lib/openpgp/client/gnupg.rb', line 319

def check_trustdb
  raise NotImplementedError # TODO
end

#clearsignObject

Makes a clear text signature.

Raises:

  • (NotImplementedError)


69
70
71
# File 'lib/openpgp/client/gnupg.rb', line 69

def clearsign
  raise NotImplementedError # TODO
end

#dearmor(file) ⇒ Object

Unpacks an arbitrary input from an OpenPGP ASCII armor.



405
406
407
408
# File 'lib/openpgp/client/gnupg.rb', line 405

def dearmor(file)
  data = OpenPGP.dearmor(File.read(file))
  puts data # FIXME
end

#decrypt(file) ⇒ Object

Decrypts data.

Raises:

  • (NotImplementedError)


112
113
114
# File 'lib/openpgp/client/gnupg.rb', line 112

def decrypt(file)
  raise NotImplementedError # TODO
end

#decrypt_files(*files) ⇒ Object

Identical to –multifile –decrypt.



138
139
140
141
# File 'lib/openpgp/client/gnupg.rb', line 138

def decrypt_files(*files)
  options[:multifile] = true
  files.each { |file| decrypt(file) }
end

#delete_key(name) ⇒ Object

Removes key from the public keyring.

Raises:

  • (NotImplementedError)


234
235
236
# File 'lib/openpgp/client/gnupg.rb', line 234

def delete_key(name)
  raise NotImplementedError # TODO
end

#delete_secret_and_public_key(name) ⇒ Object

Removes key from the secret and public keyring. If a secret key exists, it will be removed first.

Raises:

  • (NotImplementedError)


246
247
248
# File 'lib/openpgp/client/gnupg.rb', line 246

def delete_secret_and_public_key(name)
  raise NotImplementedError # TODO
end

#delete_secret_key(name) ⇒ Object

Removes key from the secret and public keyring.

Raises:

  • (NotImplementedError)


240
241
242
# File 'lib/openpgp/client/gnupg.rb', line 240

def delete_secret_key(name)
  raise NotImplementedError # TODO
end

#desig_revoke(name) ⇒ Object

Generates a designated revocation certificate for a key.

Raises:

  • (NotImplementedError)


426
427
428
# File 'lib/openpgp/client/gnupg.rb', line 426

def desig_revoke(name)
  raise NotImplementedError # TODO
end

#detach_signObject

Makes a detached signature.

Raises:

  • (NotImplementedError)


75
76
77
# File 'lib/openpgp/client/gnupg.rb', line 75

def detach_sign
  raise NotImplementedError # TODO
end

#dump_optionsObject

Prints a list of all available options and commands.



50
51
52
53
54
55
56
57
# File 'lib/openpgp/client/gnupg.rb', line 50

def dump_options
  self.class.public_instance_methods(false).each do |command|
    if command =~ /^[\w\d_]+$/
      puts "--#{command.to_s.gsub('_', '-')}"
    end
  end
  # TODO: list available options, too.
end

#edit_key(key) ⇒ Object

Present a menu which enables you to do most of the key management related tasks.

Raises:

  • (NotImplementedError)


432
433
434
# File 'lib/openpgp/client/gnupg.rb', line 432

def edit_key(key)
  raise NotImplementedError # TODO
end

#enarmor(file) ⇒ Object

Packs an arbitrary input into an OpenPGP ASCII armor.



398
399
400
401
# File 'lib/openpgp/client/gnupg.rb', line 398

def enarmor(file)
  text = OpenPGP.enarmor(File.read(file), :armored_file, :comment => 'Use "gpg --dearmor" for unpacking', :line_length => 64)
  puts text # FIXME
end

#encryptObject

Encrypts data.

Raises:

  • (NotImplementedError)


81
82
83
# File 'lib/openpgp/client/gnupg.rb', line 81

def encrypt
  raise NotImplementedError # TODO
end

#encrypt_files(*files) ⇒ Object

Identical to –multifile –encrypt.



131
132
133
134
# File 'lib/openpgp/client/gnupg.rb', line 131

def encrypt_files(*files)
  options[:multifile] = true
  files.each { |file| encrypt(file) }
end

#export(*keys) ⇒ Object

Exports keys from the public keyring.

Raises:

  • (NotImplementedError)


252
253
254
# File 'lib/openpgp/client/gnupg.rb', line 252

def export(*keys)
  raise NotImplementedError # TODO
end

#export_ownertrustObject

Sends the ownertrust values to stdout.

Raises:

  • (NotImplementedError)


325
326
327
# File 'lib/openpgp/client/gnupg.rb', line 325

def export_ownertrust
  raise NotImplementedError # TODO
end

#export_secret_keysObject

Exports the secret keys.

Raises:

  • (NotImplementedError)


264
265
266
# File 'lib/openpgp/client/gnupg.rb', line 264

def export_secret_keys
  raise NotImplementedError # TODO
end

#export_secret_subkeysObject

Exports the secret subkeys.

Raises:

  • (NotImplementedError)


270
271
272
# File 'lib/openpgp/client/gnupg.rb', line 270

def export_secret_subkeys
  raise NotImplementedError # TODO
end

#fast_import(*keys) ⇒ Object

Alias for import.



282
283
284
# File 'lib/openpgp/client/gnupg.rb', line 282

def fast_import(*keys)
  import(*keys)
end

#fetch_keys(*uris) ⇒ Object

Retrieves keys located at the specified URIs.

Raises:

  • (NotImplementedError)


306
307
308
309
# File 'lib/openpgp/client/gnupg.rb', line 306

def fetch_keys(*uris)
  require 'open-uri'
  raise NotImplementedError # TODO
end

#fingerprint(*keys) ⇒ Object

Lists all keys (or the specified ones) along with their fingerprints.



203
204
205
206
# File 'lib/openpgp/client/gnupg.rb', line 203

def fingerprint(*keys)
  options[:fingerprint] = true
  list_keys(*keys)
end

#gen_keyObject

Generates a new key pair.

Raises:

  • (NotImplementedError)


414
415
416
# File 'lib/openpgp/client/gnupg.rb', line 414

def gen_key
  raise NotImplementedError # TODO
end

#gen_prime(mode, bits, qbits = nil) ⇒ Object

Generates a prime number.



387
388
389
390
391
392
393
394
# File 'lib/openpgp/client/gnupg.rb', line 387

def gen_prime(mode, bits, qbits = nil)
  case mode.to_i
    when 1..4
      raise NotImplementedError # TODO
    else
      wrong_args "--gen-prime mode bits [qbits]"
  end
end

#gen_random(level = 0, count = nil) ⇒ Object

Emits count random bytes of the given quality level.



370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/openpgp/client/gnupg.rb', line 370

def gen_random(level = 0, count = nil)
  wrong_args "--gen-random 0|1|2 [count]" unless (0..2).include?(level)

  require 'openssl'
  count   = count.to_i if count
  endless = count.nil?
  while endless || count > 0
    n = !endless && count < 99 ? count : 99
    p = Random.bytes(n)
    print options[:armor] ? [p].pack('m').delete("\n") : p
    count -= n unless endless
  end
  puts if options[:armor]
end

#gen_revoke(name) ⇒ Object

Generates a revocation certificate for the complete key.

Raises:

  • (NotImplementedError)


420
421
422
# File 'lib/openpgp/client/gnupg.rb', line 420

def gen_revoke(name)
  raise NotImplementedError # TODO
end

#helpObject

Prints a usage message summarizing the most useful command-line options.



40
# File 'lib/openpgp/client/gnupg.rb', line 40

def help() end

#import(*keys) ⇒ Object

Imports/merges keys, adding the given keys to the keyring.

Raises:

  • (NotImplementedError)


276
277
278
# File 'lib/openpgp/client/gnupg.rb', line 276

def import(*keys)
  raise NotImplementedError # TODO
end

#import_ownertrust(*files) ⇒ Object

Updates the trustdb with the ownertrust values stored in files or stdin.

Raises:

  • (NotImplementedError)


331
332
333
# File 'lib/openpgp/client/gnupg.rb', line 331

def import_ownertrust(*files)
  raise NotImplementedError # TODO
end

#list_keys(*keys) ⇒ Object

Lists keys from the public keyrings.



145
146
147
# File 'lib/openpgp/client/gnupg.rb', line 145

def list_keys(*keys)
  list_public_keys(*keys)
end

#list_packetsObject

Lists only the sequence of packets.

Raises:

  • (NotImplementedError)


210
211
212
# File 'lib/openpgp/client/gnupg.rb', line 210

def list_packets
  raise NotImplementedError # TODO
end

#list_public_keys(*keys) ⇒ Object

Lists keys from the public keyrings.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/openpgp/client/gnupg.rb', line 151

def list_public_keys(*keys)
  public_keyrings.each do |keyring_filename, keyring|
    puts (keyring_filename = File.expand_path(keyring_filename))
    print '-' * keyring_filename.size

    keyring.each do |packet|
      case packet
        when Packet::PublicSubkey
          print_key_listing(packet, :sub)
        when Packet::PublicKey
          print_key_listing(packet, :pub)
        when Packet::UserID
          print_uid_listing(packet)
      end
    end
  end
end

#list_secret_keys(*keys) ⇒ Object

Lists keys from the secret keyrings.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/openpgp/client/gnupg.rb', line 171

def list_secret_keys(*keys)
  secret_keyrings.each do |keyring_filename, keyring|
    puts (keyring_filename = File.expand_path(keyring_filename))
    print '-' * keyring_filename.size

    keyring.each do |packet|
      case packet
        when Packet::SecretSubkey
          print_key_listing(packet, :ssb)
        when Packet::SecretKey
          print_key_listing(packet, :sec)
        when Packet::UserID
          print_uid_listing(packet)
      end
    end
  end
end

#list_sigsObject

Same as list_keys, but the signatures are listed too.

Raises:

  • (NotImplementedError)


191
192
193
# File 'lib/openpgp/client/gnupg.rb', line 191

def list_sigs
  raise NotImplementedError # TODO
end

#lsign_key(name) ⇒ Object

Signs a public key with your secret key but marks it as non-exportable.

Raises:

  • (NotImplementedError)


444
445
446
# File 'lib/openpgp/client/gnupg.rb', line 444

def lsign_key(name)
  raise NotImplementedError # TODO
end

Prints message digest of algorithm algo for all given files or stdin.



343
344
345
346
347
348
349
350
351
352
353
# File 'lib/openpgp/client/gnupg.rb', line 343

def print_md(algo, *files)
  unless digest_algorithms.include?(algorithm = algo.to_s.upcase.to_sym)
    abort "gpg: invalid hash algorithm `#{algo}'"
  else
    digest = Digest.for(algorithm)
  end

  files.each do |file|
    puts (prefix = "#{file}: ") << format_fingerprint(digest.file(file).hexdigest, prefix.size)
  end
end

Prints message digests of all available algorithms for all given files or stdin.



357
358
359
360
361
362
363
364
365
366
# File 'lib/openpgp/client/gnupg.rb', line 357

def print_mds(*files)
  files.each do |file|
    digest_algorithms.each do |algorithm|
      algorithm = :RMD160 if algorithm == :RIPEMD160
      digest    = Digest.for(algorithm)

      puts (prefix = "#{file}: #{algorithm.to_s.rjust(6)} = ") << format_fingerprint(digest.file(file).hexdigest, prefix.size)
    end
  end
end

#rebuild_keydb_cachesObject

Creates signature caches in the keyring.

Raises:

  • (NotImplementedError)


337
338
339
# File 'lib/openpgp/client/gnupg.rb', line 337

def rebuild_keydb_caches
  raise NotImplementedError # TODO
end

#recv_keys(*keys) ⇒ Object

Imports the keys with the given key IDs from a keyserver.

Raises:

  • (NotImplementedError)


288
289
290
# File 'lib/openpgp/client/gnupg.rb', line 288

def recv_keys(*keys)
  raise NotImplementedError # TODO
end

#refresh_keys(*keys) ⇒ Object

Requests updates from a keyserver for keys that already exist on the local keyring.

Raises:

  • (NotImplementedError)


294
295
296
# File 'lib/openpgp/client/gnupg.rb', line 294

def refresh_keys(*keys)
  raise NotImplementedError # TODO
end

#search_keys(*names) ⇒ Object

Searches the keyserver for the given names.

Raises:

  • (NotImplementedError)


300
301
302
# File 'lib/openpgp/client/gnupg.rb', line 300

def search_keys(*names)
  raise NotImplementedError # TODO
end

#send_keys(*keys) ⇒ Object

Sends keys to a keyserver.

Raises:

  • (NotImplementedError)


258
259
260
# File 'lib/openpgp/client/gnupg.rb', line 258

def send_keys(*keys)
  raise NotImplementedError # TODO
end

#signObject

Makes a signature.

Raises:

  • (NotImplementedError)


63
64
65
# File 'lib/openpgp/client/gnupg.rb', line 63

def sign
  raise NotImplementedError # TODO
end

#sign_key(name) ⇒ Object

Signs a public key with your secret key.

Raises:

  • (NotImplementedError)


438
439
440
# File 'lib/openpgp/client/gnupg.rb', line 438

def sign_key(name)
  raise NotImplementedError # TODO
end

#store(file) ⇒ Object

Stores only (make a simple RFC1991 literal data packet).



99
100
101
102
103
104
105
106
107
108
# File 'lib/openpgp/client/gnupg.rb', line 99

def store(file)
  Message.write(stdout) do |msg|
    msg << Packet::LiteralData.new({
      :format    => :b,
      :filename  => File.basename(file),
      :timestamp => File.mtime(file),
      :data      => File.read(file),
    })
  end
end

#symmetric(file) ⇒ Object

Encrypts with a symmetric cipher using a passphrase.



87
88
89
90
91
92
93
94
95
# File 'lib/openpgp/client/gnupg.rb', line 87

def symmetric(file)
  print OpenPGP.encrypt(File.read(file), {
    :symmetric  => true,
    :passphrase => read_passphrase,
    :cipher     => cipher_algorithm,
    :digest     => digest_algorithm,
    :compress   => compress_algorithm,
  })
end

#update_trustdbObject

Does trust database maintenance.

Raises:

  • (NotImplementedError)


313
314
315
# File 'lib/openpgp/client/gnupg.rb', line 313

def update_trustdb
  raise NotImplementedError # TODO
end

#verify(file) ⇒ Object

Verifies data.

Raises:

  • (NotImplementedError)


118
119
120
# File 'lib/openpgp/client/gnupg.rb', line 118

def verify(file)
  raise NotImplementedError # TODO
end

#verify_files(*files) ⇒ Object

Identical to –multifile –verify.



124
125
126
127
# File 'lib/openpgp/client/gnupg.rb', line 124

def verify_files(*files)
  options[:multifile] = true
  files.each { |file| verify(file) }
end

#versionObject

Prints the program version and licensing information.



27
28
29
30
31
32
33
34
35
36
# File 'lib/openpgp/client/gnupg.rb', line 27

def version
  puts "gpg.rb (GnuPG compatible) #{VERSION}"
  puts
  puts "Home: #{options[:homedir]}"
  puts "Supported algorithms:"
  puts "Pubkey: " # TODO
  puts "Cipher: #{cipher_algorithms.keys.map(&:to_s).sort.join(', ')}"
  puts "Hash: #{digest_algorithms.join(', ')}"
  puts "Compression: #{compress_algorithms.keys.map(&:to_s).sort.join(', ')}"
end

#warrantyObject

Prints warranty information.

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/openpgp/client/gnupg.rb', line 44

def warranty
  raise NotImplementedError
end