module Gpgr::Keys

Encapsulates all the functionality for dealing with GPG Keys. There isn't much here since key managment isn't really one of the goals of this project. It will, however, allow you to import new keys and provides a means to list existing installed keys.

Public Class Methods

import(path_to_key) click to toggle source

Imports the key at the specified path into the keyring. Since this is really running gpg –import ./path/to/key.asc, the key will be imported and added to the keyring for the user executing this command.

# File lib/gpgr.rb, line 143
def self.import(path_to_key)
  system "#{Gpgr.command} -q --no-verbose --yes --import #{File.expand_path(path_to_key)}"
end
import_keys_at(path) click to toggle source

Iterates through all of the files at a specified path and attempts to import those which are likely to be GPG / PGP Public Keys.

# File lib/gpgr.rb, line 150
def self.import_keys_at(path)
  Dir.new(path).each do |file|
    next if ['..','.'].include?(file)
    Gpgr::Keys.import(path + '/' + file)
  end
end
installed_public_keys() click to toggle source

Returns an array with the e-mail addresses of every installed public key for looping through and detecting if a particular key is installed.

# File lib/gpgr.rb, line 160
def self.installed_public_keys
  keys = []
  email_regexp = /\<(.*@.*)\>/

  # Select the output to grep for, which is different depending on the version
  # of GPG installed. This is tested on 1.4 and 2.1.
  #
  if %x`#{Gpgr.command} --version | grep GnuPG`.include?('1.')
    grep_for = 'pub'
  else
    grep_for = 'uid'
  end

  %x`#{Gpgr.command} --list-public-keys --with-colons | grep #{grep_for}`.split("\n").each do |key| 
    keys << email_regexp.match(key)[1].upcase
  end

  keys.uniq
end
public_key_installed?(email) click to toggle source

Simply checks to see if the e-mail address passed through as an argument has a public key attached to it by checking in installed_public_keys.

# File lib/gpgr.rb, line 183
def self.public_key_installed?(email)
  installed_public_keys.include?(email.upcase)
end