class OpenPGP::Client::GnuPG

GNU Privacy Guard (GnuPG) implementation.

@see www.gnupg.org/

Constants

VERSION

Attributes

options[RW]

Public Class Methods

new(options = {}) click to toggle source
# 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

Public Instance Methods

card_edit() click to toggle source

Presents a menu to work with a smartcard.

@return [void]

# File lib/openpgp/client/gnupg.rb, line 274
def card_edit
  raise NotImplementedError # TODO
end
card_status() click to toggle source

Shows the content of the smart card.

@return [void]

# File lib/openpgp/client/gnupg.rb, line 282
def card_status
  raise NotImplementedError # TODO
end
change_pin() click to toggle source

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

@return [void]

# File lib/openpgp/client/gnupg.rb, line 290
def change_pin
  raise NotImplementedError # TODO
end
check_sigs() click to toggle source

Same as {#list_sigs}, but the signatures are verified.

@return [void]

# File lib/openpgp/client/gnupg.rb, line 248
def check_sigs
  raise NotImplementedError # TODO
end
check_trustdb() click to toggle source

Does trust database maintenance without user interaction.

@return [void]

# File lib/openpgp/client/gnupg.rb, line 424
def check_trustdb
  raise NotImplementedError # TODO
end
clearsign() click to toggle source

Makes a clear text signature.

@return [void]

# File lib/openpgp/client/gnupg.rb, line 82
def clearsign
  raise NotImplementedError # TODO
end
dearmor(file) click to toggle source

Unpacks an arbitrary input from an OpenPGP ASCII armor.

@param [String, to_s] file @return [void]

# File lib/openpgp/client/gnupg.rb, line 542
def dearmor(file)
  data = OpenPGP.dearmor(File.read(file))
  puts data # FIXME
end
decrypt(file) click to toggle source

Decrypts data.

@param [String, to_s] file @return [void]

# File lib/openpgp/client/gnupg.rb, line 138
def decrypt(file)
  raise NotImplementedError # TODO
end
decrypt_files(*files) click to toggle source

Identical to `–multifile –decrypt`.

@param [Array<String>] files @return [void]

# File lib/openpgp/client/gnupg.rb, line 176
def decrypt_files(*files)
  options[:multifile] = true
  files.each { |file| decrypt(file) }
end
delete_key(name) click to toggle source

Removes key from the public keyring.

@param [String, to_s] name @return [void]

# File lib/openpgp/client/gnupg.rb, line 299
def delete_key(name)
  raise NotImplementedError # TODO
end
delete_secret_and_public_key(name) click to toggle source

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

@param [String, to_s] name @return [void]

# File lib/openpgp/client/gnupg.rb, line 318
def delete_secret_and_public_key(name)
  raise NotImplementedError # TODO
end
delete_secret_key(name) click to toggle source

Removes key from the secret and public keyring.

@param [String, to_s] name @return [void]

# File lib/openpgp/client/gnupg.rb, line 308
def delete_secret_key(name)
  raise NotImplementedError # TODO
end
desig_revoke(name) click to toggle source

Generates a designated revocation certificate for a key.

@param [String, to_s] name @return [void]

# File lib/openpgp/client/gnupg.rb, line 571
def desig_revoke(name)
  raise NotImplementedError # TODO
end
detach_sign() click to toggle source

Makes a detached signature.

@return [void]

# File lib/openpgp/client/gnupg.rb, line 90
def detach_sign
  raise NotImplementedError # TODO
end
dump_options() click to toggle source

Prints a list of all available options and commands.

@return [void]

# File lib/openpgp/client/gnupg.rb, line 59
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) click to toggle source

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

@param [String, to_s] key @return [void]

# File lib/openpgp/client/gnupg.rb, line 581
def edit_key(key)
  raise NotImplementedError # TODO
end
enarmor(file) click to toggle source

Packs an arbitrary input into an OpenPGP ASCII armor.

@param [String, to_s] file @return [void]

# File lib/openpgp/client/gnupg.rb, line 532
def enarmor(file)
  text = OpenPGP.enarmor(File.read(file), :armored_file, :comment => 'Use "gpg --dearmor" for unpacking', :line_length => 64)
  puts text # FIXME
end
encrypt() click to toggle source

Encrypts data.

@return [void]

# File lib/openpgp/client/gnupg.rb, line 98
def encrypt
  raise NotImplementedError # TODO
end
encrypt_files(*files) click to toggle source

Identical to `–multifile –encrypt`.

@param [Array<String>] files @return [void]

# File lib/openpgp/client/gnupg.rb, line 166
def encrypt_files(*files)
  options[:multifile] = true
  files.each { |file| encrypt(file) }
end
export(*keys) click to toggle source

Exports keys from the public keyring.

@param [Array<String>] keys @return [void]

# File lib/openpgp/client/gnupg.rb, line 327
def export(*keys)
  raise NotImplementedError # TODO
end
export_ownertrust() click to toggle source

Sends the ownertrust values to `stdout`.

@return [void]

# File lib/openpgp/client/gnupg.rb, line 432
def export_ownertrust
  raise NotImplementedError # TODO
end
export_secret_keys() click to toggle source

Exports the secret keys.

@return [void]

# File lib/openpgp/client/gnupg.rb, line 344
def export_secret_keys
  raise NotImplementedError # TODO
end
export_secret_subkeys() click to toggle source

Exports the secret subkeys.

@return [void]

# File lib/openpgp/client/gnupg.rb, line 352
def export_secret_subkeys
  raise NotImplementedError # TODO
end
fast_import(*keys) click to toggle source

Alias for {#import}.

@param [Array<String>] keys @return [void]

# File lib/openpgp/client/gnupg.rb, line 370
def fast_import(*keys)
  import(*keys)
end
fetch_keys(*uris) click to toggle source

Retrieves keys located at the specified URIs.

@param [Array<String>] uris @return [void]

# File lib/openpgp/client/gnupg.rb, line 407
def fetch_keys(*uris)
  require 'open-uri'
  raise NotImplementedError # TODO
end
fingerprint(*keys) click to toggle source

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

@param [Array<String>] keys @return [void]

# File lib/openpgp/client/gnupg.rb, line 257
def fingerprint(*keys)
  options[:fingerprint] = true
  list_keys(*keys)
end
gen_key() click to toggle source

Generates a new key pair.

@return [void]

# File lib/openpgp/client/gnupg.rb, line 553
def gen_key
  raise NotImplementedError # TODO
end
gen_prime(mode, bits, qbits = nil) click to toggle source

Generates a prime number.

@param [Integer, to_i] mode @param [Integer, to_i] bits @param [Integer, to_i] qbits @return [void]

# File lib/openpgp/client/gnupg.rb, line 518
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) click to toggle source

Emits `count` random bytes of the given quality level.

@param [Integer, to_i] level @param [Integer, to_i] count @return [void]

# File lib/openpgp/client/gnupg.rb, line 496
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) click to toggle source

Generates a revocation certificate for the complete key.

@param [String, to_s] name @return [void]

# File lib/openpgp/client/gnupg.rb, line 562
def gen_revoke(name)
  raise NotImplementedError # TODO
end
help() click to toggle source

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

@return [void]

# File lib/openpgp/client/gnupg.rb, line 45
def help() end
import(*keys) click to toggle source

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

@param [Array<String>] keys @return [void]

# File lib/openpgp/client/gnupg.rb, line 361
def import(*keys)
  raise NotImplementedError # TODO
end
import_ownertrust(*files) click to toggle source

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

@param [Array<String>] files @return [void]

# File lib/openpgp/client/gnupg.rb, line 442
def import_ownertrust(*files)
  raise NotImplementedError # TODO
end
list_keys(*keys) click to toggle source

Lists keys from the public keyrings.

@param [Array<String>] keys @return [void]

# File lib/openpgp/client/gnupg.rb, line 186
def list_keys(*keys)
  list_public_keys(*keys)
end
list_packets() click to toggle source

Lists only the sequence of packets.

@return [void]

# File lib/openpgp/client/gnupg.rb, line 266
def list_packets
  raise NotImplementedError # TODO
end
list_public_keys(*keys) click to toggle source

Lists keys from the public keyrings.

@param [Array<String>] keys @return [void]

# File lib/openpgp/client/gnupg.rb, line 195
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) click to toggle source

Lists keys from the secret keyrings.

@param [Array<String>] keys @return [void]

# File lib/openpgp/client/gnupg.rb, line 218
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_sigs() click to toggle source

Same as {#list_keys}, but the signatures are listed too.

@return [void]

# File lib/openpgp/client/gnupg.rb, line 240
def list_sigs
  raise NotImplementedError # TODO
end
lsign_key(name) click to toggle source

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

@param [String, to_s] name @return [void]

# File lib/openpgp/client/gnupg.rb, line 600
def lsign_key(name)
  raise NotImplementedError # TODO
end
print_md(algo, *files) click to toggle source

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

@param [String, to_s] algo @param [Array<String>] files @return [void]

print_mds(*files) click to toggle source

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

@param [Array<String>] files @return [void]

rebuild_keydb_caches() click to toggle source

Creates signature caches in the keyring.

@return [void]

# File lib/openpgp/client/gnupg.rb, line 450
def rebuild_keydb_caches
  raise NotImplementedError # TODO
end
recv_keys(*keys) click to toggle source

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

@param [Array<String>] keys @return [void]

# File lib/openpgp/client/gnupg.rb, line 379
def recv_keys(*keys)
  raise NotImplementedError # TODO
end
refresh_keys(*keys) click to toggle source

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

@param [Array<String>] keys @return [void]

# File lib/openpgp/client/gnupg.rb, line 389
def refresh_keys(*keys)
  raise NotImplementedError # TODO
end
search_keys(*names) click to toggle source

Searches the keyserver for the given `names`.

@param [Array<String>] names @return [void]

# File lib/openpgp/client/gnupg.rb, line 398
def search_keys(*names)
  raise NotImplementedError # TODO
end
send_keys(*keys) click to toggle source

Sends keys to a keyserver.

@param [Array<String>] keys @return [void]

# File lib/openpgp/client/gnupg.rb, line 336
def send_keys(*keys)
  raise NotImplementedError # TODO
end
sign() click to toggle source

Makes a signature.

@return [void]

# File lib/openpgp/client/gnupg.rb, line 74
def sign
  raise NotImplementedError # TODO
end
sign_key(name) click to toggle source

Signs a public key with your secret key.

@param [String, to_s] name @return [void]

# File lib/openpgp/client/gnupg.rb, line 590
def sign_key(name)
  raise NotImplementedError # TODO
end
store(file) click to toggle source

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

@param [String, to_s] file @return [void]

# File lib/openpgp/client/gnupg.rb, line 122
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) click to toggle source

Encrypts with a symmetric cipher using a passphrase.

@param [String, to_s] file @return [void]

# File lib/openpgp/client/gnupg.rb, line 107
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_trustdb() click to toggle source

Does trust database maintenance.

@return [void]

# File lib/openpgp/client/gnupg.rb, line 416
def update_trustdb
  raise NotImplementedError # TODO
end
verify(file) click to toggle source

Verifies data.

@param [String, to_s] file @return [void]

# File lib/openpgp/client/gnupg.rb, line 147
def verify(file)
  raise NotImplementedError # TODO
end
verify_files(*files) click to toggle source

Identical to `–multifile –verify`.

@param [Array<String>] files @return [void]

# File lib/openpgp/client/gnupg.rb, line 156
def verify_files(*files)
  options[:multifile] = true
  files.each { |file| verify(file) }
end
version() click to toggle source

Prints the program version and licensing information.

@return [void]

# File lib/openpgp/client/gnupg.rb, line 29
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
warranty() click to toggle source

Prints warranty information.

@return [void]

# File lib/openpgp/client/gnupg.rb, line 51
def warranty
  raise NotImplementedError
end

Protected Instance Methods

cipher_algorithm() click to toggle source

@return [Symbol]

# File lib/openpgp/client/gnupg.rb, line 731
def cipher_algorithm
  unless options[:cipher_algo]
    Cipher::CAST5 # this is the default cipher
  else
    algorithm = options[:cipher_algo].to_s.upcase.to_sym
    unless cipher_algorithms.has_key?(algorithm)
      abort "gpg: selected cipher algorithm is invalid"
    end
    cipher_algorithms[algorithm]
  end
end
cipher_algorithms() click to toggle source

@return [Hash]

# File lib/openpgp/client/gnupg.rb, line 757
def cipher_algorithms
  {
    :"3DES"   => Cipher::TripleDES,
    :CAST5    => Cipher::CAST5,
    :BLOWFISH => Cipher::Blowfish,
    :AES      => Cipher::AES128,
    :AES192   => Cipher::AES192,
    :AES256   => Cipher::AES256,
    #:TWOFISH  => Cipher::Twofish, # N/A
  }
end
compress_algorithm() click to toggle source

@return [Symbol]

# File lib/openpgp/client/gnupg.rb, line 751
def compress_algorithm
  options[:compress_algo]
end
compress_algorithms() click to toggle source

@return [Hash]

# File lib/openpgp/client/gnupg.rb, line 777
def compress_algorithms
  {
    :none     => nil,
    :ZIP      => nil, # TODO
    :ZLIB     => nil, # TODO
    :BZIP2    => nil, # TODO
  }
end
digest_algorithm() click to toggle source

@return [Symbol]

# File lib/openpgp/client/gnupg.rb, line 745
def digest_algorithm
  options[:digest_algo]
end
digest_algorithms() click to toggle source

@return [Array]

# File lib/openpgp/client/gnupg.rb, line 771
def digest_algorithms
  [:MD5, :SHA1, :RIPEMD160, :SHA256, :SHA384, :SHA512]
end
format_fingerprint(input, column = 0) click to toggle source

@param [String] input @param [Integer] column @return [String]

# File lib/openpgp/client/gnupg.rb, line 695
def format_fingerprint(input, column = 0)
  group_size = case input.size
    when 32 then 2 # MD5
    when 40 then 4 # SHA1, RIPEMD160
            else 8 # SHA2*
  end

  lines, line, pos = [], '', 0
  input.upcase!
  input.each_byte do |c|
    line << c
    if (pos += 1) % group_size == 0
      if (line.size + column) >= (80 - group_size)
        lines << line
        line, pos = '', 0
      else
        line << ' '
      end
    end
  end
  lines << line.strip unless line.empty?

  output = lines.join($/ + (' ' * column))
  output = output.insert(output.size / 2, ' ') if group_size < 8
  return output
end
format_keyspec(key) click to toggle source

@param [Packet] key @return [String]

# File lib/openpgp/client/gnupg.rb, line 687
def format_keyspec(key)
  "____?/#{key.key_id}" # TODO
end
keyring(file) click to toggle source

@param [String, to_s] file @return [Message]

# File lib/openpgp/client/gnupg.rb, line 643
def keyring(file)
  OpenPGP::Message.parse(File.read(File.expand_path(file)))
end
parse_options_file(file) click to toggle source

@param [String, to_s] file @return [void]

# File lib/openpgp/client/gnupg.rb, line 725
def parse_options_file(file)
  # TODO
end
print_key_listing(packet, type) click to toggle source

@param [Packet] packet @param [Symbol, to_sym] type @return [void]

print_uid_listing(packet) click to toggle source

@param [Packet, to_s] packet @return [void]

public_keyring_file() click to toggle source

@return [String]

# File lib/openpgp/client/gnupg.rb, line 649
def public_keyring_file
  File.join(options[:homedir], 'pubring.gpg')
end
public_keyrings() click to toggle source

@return [Hash]

# File lib/openpgp/client/gnupg.rb, line 630
def public_keyrings
  {public_keyring_file => keyring(public_keyring_file)} # FIXME
end
read_passphrase() click to toggle source

@return [String]

# File lib/openpgp/client/gnupg.rb, line 620
def read_passphrase
  if options[:passphrase]
    options[:passphrase]
  else
    # TODO
  end
end
secret_keyring_file() click to toggle source

@return [String]

# File lib/openpgp/client/gnupg.rb, line 655
def secret_keyring_file
  File.join(options[:homedir], 'secring.gpg')
end
secret_keyrings() click to toggle source

@return [Hash]

# File lib/openpgp/client/gnupg.rb, line 636
def secret_keyrings
  {secret_keyring_file => keyring(secret_keyring_file)} # FIXME
end
stderr() click to toggle source

@return [IO]

# File lib/openpgp/client/gnupg.rb, line 616
def stderr() $stdout end
stdin() click to toggle source

@return [IO]

# File lib/openpgp/client/gnupg.rb, line 608
def stdin()  $stdin  end
stdout() click to toggle source

@return [IO]

# File lib/openpgp/client/gnupg.rb, line 612
def stdout() $stdout end
trustdb_file() click to toggle source

@return [String]

# File lib/openpgp/client/gnupg.rb, line 661
def trustdb_file
  File.join(options[:homedir], 'trustdb.gpg')
end
wrong_args(usage) click to toggle source

@param [String, to_s] usage @return [void]

# File lib/openpgp/client/gnupg.rb, line 789
def wrong_args(usage)
  abort "usage: gpg.rb [options] #{usage}"
end