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
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 145 def self.import(path_to_key) system "#{Gpgr.command} -q --no-verbose --yes --import #{File.expand_path(path_to_key)}" end
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 152 def self.import_keys_at(path) Dir.new(path).each do |file| next if ['..','.'].include?(file) Gpgr::Keys.import(path + '/' + file) end end
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 162 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 `#{Gpgr.command} --version | grep GnuPG`.include?('2.') grep_for = 'uid' else grep_for = 'pub' end `#{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
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 185 def self.public_key_installed?(email) installed_public_keys.include?(email.upcase) end