class OpenPGP::Engine::GnuPG

GNU Privacy Guard (GnuPG) wrapper.

@see www.gnupg.org/

Constants

OPTIONS

Attributes

options[RW]

@return [Hash{Symbol => Object}]

where[RW]

@return [String]

Public Class Methods

available?() click to toggle source

@return [Boolean]

# File lib/openpgp/engine/gnupg.rb, line 11
def self.available?
  self.new.available?
end
new(options = {}) click to toggle source

@param [Hash{Symbol => Object}] options

# File lib/openpgp/engine/gnupg.rb, line 32
def initialize(options = {})
  @where   = '/usr/bin/env gpg' # FIXME
  @options = OPTIONS.merge!(options)
end

Public Instance Methods

available?() click to toggle source

Determines if GnuPG is available.

@return [Boolean]

# File lib/openpgp/engine/gnupg.rb, line 41
def available?
  !!version
end
clearsign() click to toggle source

Makes a clear text OpenPGP signature.

# File lib/openpgp/engine/gnupg.rb, line 161
def clearsign()
  # TODO
end
decrypt(ciphertext, options = {}) click to toggle source

Decrypts the given ciphertext using the specified key ID.

@param [String] ciphertext @param [Hash{Symbol => Object}] options @return [String]

# File lib/openpgp/engine/gnupg.rb, line 141
def decrypt(ciphertext, options = {})
  # TODO
end
delete_secret_and_public_key(key_id) click to toggle source

@param [String] key_id @return [void]

# File lib/openpgp/engine/gnupg.rb, line 100
def delete_secret_and_public_key(key_id)
  opts = {:batch => true}
  OpenPGP::Message.parse(exec([:delete_secret_and_public_key, key_fingerprint(key_id)], opts ).read)
end
detach_sign() click to toggle source

Makes a detached OpenPGP signature.

# File lib/openpgp/engine/gnupg.rb, line 167
def detach_sign()
  # TODO
end
encrypt(plaintext, options = {}) click to toggle source

Encrypts the given plaintext to the specified recipients.

@param [String] plaintext @param [Hash{Symbol => Object}] options @return [String]

# File lib/openpgp/engine/gnupg.rb, line 131
def encrypt(plaintext, options = {})
  # TODO
end
exec(command, options = {}) { |stdin| ... } click to toggle source

Executes a GnuPG command, yielding the standard input and returning the standard output.

@param [String] command @param [Hash{Symbol => Object}] options @return [IO]

# File lib/openpgp/engine/gnupg.rb, line 184
def exec(command, options = {}, &block) #:yields: stdin
  exec4(command, options) do |pid, stdin, stdout, stderr|
    block.call(stdin) if block_given?
    stdin.close_write
    pid, status = Process.waitpid2(pid)
    raise Error, stderr.read.chomp if status.exitstatus.nonzero?
    stdout
  end
end
exec3(command, options = {}) { |stdin, stdout, stderr| ... } click to toggle source

Executes a GnuPG command, yielding and returning the standard input, output and error.

@param [String] command @param [Hash{Symbol => Object}] options @return [Array(IO, IO, IO)]

# File lib/openpgp/engine/gnupg.rb, line 201
def exec3(command, options = {}, &block) #:yields: stdin, stdout, stderr
  exec4(command, options) do |pid, stdin, stdout, stderr|
    block.call(stdin, stdout, stderr) if block_given?
    stdin.close_write
    pid, status = Process.waitpid2(pid)
    raise Error, stderr.read.chomp if status.exitstatus.nonzero?
    [stdin, stdout, stderr]
  end
end
exec4(command, options = {}) { |pid, stdin, stdout, stderr| ... } click to toggle source

Executes a GnuPG command, yielding the process identifier as well as the standard input, output and error.

@param [String] command @param [Hash{Symbol => Object}] options @return [void]

# File lib/openpgp/engine/gnupg.rb, line 218
def exec4(command, options = {}, &block) #:yields: pid, stdin, stdout, stderr
  require 'rubygems'
  require 'open4'
  block.call(*Open4.popen4(cmdline(command, options)))
end
export(key_id = nil, opts = {}) click to toggle source

Exports a specified key from the GnuPG keyring.

@param [String] key_id @param [Hash{Symbol => Object}] options @return [Message]

# File lib/openpgp/engine/gnupg.rb, line 85
def export(key_id = nil, opts = {})
  OpenPGP::Message.parse(exec([:export, *[key_id].flatten], opts ).read)
end
gen_key(info = {}) click to toggle source

Generates a new OpenPGP keypair and stores it GnuPG’s keyring.

@param [Hash{Symbol => String}] info @return [Integer]

# File lib/openpgp/engine/gnupg.rb, line 58
def gen_key(info = {})
  stdin, stdout, stderr = exec3(:gen_key) do |stdin, stdout, stderr|
    stdin.puts "Key-Type: #{info[:key_type]}"           if info[:key_type]
    stdin.puts "Key-Length: #{info[:key_length]}"       if info[:key_length]
    stdin.puts "Subkey-Type: #{info[:subkey_type]}"     if info[:subkey_type]
    stdin.puts "Subkey-Length: #{info[:subkey_length]}" if info[:subkey_length]
    stdin.puts "Name-Real: #{info[:name]}"              if info[:name]
    stdin.puts "Name-Comment: #{info[:comment]}"        if info[:comment]
    stdin.puts "Name-Email: #{info[:email]}"            if info[:email]
    stdin.puts "Expire-Date: #{info[:expire_date]}"     if info[:expire_date]
    stdin.puts "Passphrase: #{info[:passphrase]}"       if info[:passphrase]
    stdin.puts "%commit"
  end
  stderr.each_line do |line|
    if (line = line.chomp) =~ /^gpg: key ([0-9A-F]+) marked as ultimately trusted/
      return $1.to_i(16) # the key ID
    end
  end
  return nil
end
import() click to toggle source

Imports a specified keyfile into the GnuPG keyring.

@return [void]

# File lib/openpgp/engine/gnupg.rb, line 93
def import()
  # TODO
end
key_fingerprint(key_id, opts = {}) click to toggle source

@param [String] key_id @param [Hash{Symbol => Object}] options @return [String]

# File lib/openpgp/engine/gnupg.rb, line 109
def key_fingerprint(key_id, opts = {})
  message = exec([:fingerprint, *[key_id].flatten], opts ).read
  if message =~ /Key fingerprint = (.*)\n/
    return $1.delete(" ")
  end
  nil
end
list_keys() click to toggle source

Returns an array of key IDs/titles of the keys in the public keyring.

@return [Array]

# File lib/openpgp/engine/gnupg.rb, line 121
def list_keys()
  # TODO
end
sign() click to toggle source

Makes an OpenPGP signature.

@return [void]

# File lib/openpgp/engine/gnupg.rb, line 149
def sign()
  # TODO
end
sign_file(key_id, file, passphrase) click to toggle source

Makes an OpenPGP signature.

# File lib/openpgp/engine/gnupg.rb, line 155
def sign_file(key_id, file, passphrase)
  OpenPGP::Message.parse(exec([:sign, file],{ :local_user => key_id, :passphrase => passphrase}).read)
end
verify(key_id, file) click to toggle source

Verifies an OpenPGP signature.

# File lib/openpgp/engine/gnupg.rb, line 173
def verify(key_id, file)
  OpenPGP::Message.parse(exec([:verify, file],{ :local_user => key_id}).read)
end
version() click to toggle source

Returns the GnuPG version number.

@return [String]

# File lib/openpgp/engine/gnupg.rb, line 49
def version
  exec(:version).readline =~ /^gpg \(GnuPG\) (.*)$/ ? $1 : nil
end

Protected Instance Methods

cmdline(command, options = {}) click to toggle source

Constructs the GnuPG command-line for use with exec.

@param [String] command @param [Hash{Symbol => Object}] options @return [String]

# File lib/openpgp/engine/gnupg.rb, line 232
def cmdline(command, options = {})
  command = [command].flatten
  cmdline = [where]
  cmdline += @options.merge(options).map { |k, v| !v ? nil : "#{option(k)} #{v == true ? '' : v.to_s}".rstrip }.compact
  cmdline << option(command.shift)
  cmdline += command
  cmdline.flatten.join(' ').strip
end
option(option) click to toggle source

Translates Ruby symbols into GnuPG option arguments.

@param [String, to_s] option @return [String]

# File lib/openpgp/engine/gnupg.rb, line 246
def option(option)
  "--" << option.to_s.gsub('_', '-')
end