Class: Hkp

Inherits:
Object
  • Object
show all
Defined in:
lib/hkp.rb

Overview

simple HKP client for public key retrieval

Instance Method Summary collapse

Constructor Details

#initialize(keyserver = nil) ⇒ Hkp

Returns a new instance of Hkp.



6
7
8
# File 'lib/hkp.rb', line 6

def initialize(keyserver = nil)
  @keyserver = keyserver || lookup_keyserver || 'http://pool.sks-keyservers.net:11371'
end

Instance Method Details

#fetch(id) ⇒ Object

returns the key data as returned from the server as a string



31
32
33
34
35
# File 'lib/hkp.rb', line 31

def fetch(id)
  open("#{@keyserver}/pks/lookup?options=mr&op=get&search=0x#{URI.escape id}") do |f|
    return clean_key f.read
  end
end

#fetch_and_import(id) ⇒ Object

fetches key data by id and imports the found key(s) into GPG, returning the full hex fingerprints of the imported key(s) as an array. Given there are no collisions with the id given / the server has returned exactly one key this will be a one element array.



40
41
42
43
44
# File 'lib/hkp.rb', line 40

def fetch_and_import(id)
  if key = fetch(id)
    GPGME::Key.import(key).imports.map(&:fpr)
  end
end

#search(name) ⇒ Object

hkp.search ‘[email protected]’ will return an array of arrays, one for each matching key found, containing the key id as the first elment and any further info returned by the key server in the following elements. see tools.ietf.org/html/draft-shaw-openpgp-hkp-00#section-5.2 for what that might be. unfortunately key servers seem to differ in how much and what info they return besides the key id



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hkp.rb', line 17

def search(name)
  [].tap do |results|
    open("#{@keyserver}/pks/lookup?options=mr&search=#{URI.escape name}") do |f|
      f.each_line do |l|
        components = l.strip.split(':')
        if components.shift == 'pub'
          results << components
        end
      end
    end
  end
end