class GPGME::GpgmeKey

A ruby representation of a public or a secret key.

Every key has two instances of {GPGME::SubKey}, accessible through {.subkeys}, and with a {.primary_subkey} where most attributes are derived from, like the fingerprint.

Also, every key has at least a {GPGME::UserID}, accessible through {.uids}, with a {.primary_uid}, where other attributes are derived from, like email or name

Attributes

chain_id[R]
issuer_name[R]
issuer_serial[R]
keylist_mode[R]
owner_trust[R]
protocol[R]
subkeys[R]
uids[R]

Public Class Methods

export(pattern, options = {}) click to toggle source

Exports public keys

GPGME::Key.export pattern, options

Private keys cannot be exported due to GPGME restrictions.

@param pattern

Identifier of the key to export.

@param [Hash] options

* +:output+ specify where to write the key to. It will be converted to
  a {GPGME::Data}, so it could be a file, for example.
* Any other option accepted by {GPGME::Ctx.new}

@return [GPGME::Data] the exported key.

@example

key = GPGME::Key.export "mrsimo@example.com"

@example writing to a file

out = File.open("my.key", "w+")
GPGME::Key.export "mrsimo@example.com", :output => out
# File lib/gpgme/key.rb, line 95
def export(pattern, options = {})
  output = Data.new(options[:output])

  GPGME::Ctx.new(options) do |ctx|
    ctx.export_keys(pattern, output)
  end

  output.seek(0)
  output
end
find(secret, keys_or_names = nil, purposes = []) click to toggle source

Returns an array of {GPGME::Key} objects that match the parameters.

  • secret set to :secret to get only secret keys, or to :public to get only public keys.

  • keys_or_names an array or an item that can be either {GPGME::Key} elements, or string identifiers like the email or the sha. Leave blank to get all.

  • purposes get only keys that are usable for any of these purposes. See {GPGME::Key} for a list of possible key capabilities.

@example

GPGME::Key.find :secret # => first secret key found

@example

GPGME::Key.find(:public, "mrsimo@example.com")
# => return only public keys that match mrsimo@example.com

@example

GPGME::Key.find(:public, "mrsimo@example.com", :sign)
# => return the public keys that match mrsimo@example.com and are
#    capable of signing
# File lib/gpgme/key.rb, line 45
def find(secret, keys_or_names = nil, purposes = [])
  secret = (secret == :secret)
  keys_or_names = [""] if keys_or_names.nil? || (keys_or_names.is_a?(Array) && keys_or_names.empty?)
  keys_or_names = [keys_or_names].flatten
  purposes      = [purposes].flatten.compact.uniq

  keys = []
  keys_or_names.each do |key_or_name|
    case key_or_name
    when Key then keys << key_or_name
    when String
      GPGME::Ctx.new do |ctx|
        keys += ctx.keys(key_or_name, secret).select do |k|
          k.usable_for?(purposes)
        end
      end
    end
  end
  keys
end
get(fingerprint) click to toggle source
# File lib/gpgme/key.rb, line 66
def get(fingerprint)
  Ctx.new do |ctx|
    ctx.get_key(fingerprint)
  end
end
import(keydata, options = {}) click to toggle source

Imports a key

GPGME::Key.import keydata, options

@param keydata

The key to import. It will be converted to a {GPGME::Data} object,
so could be a file, for example.

@param options

Any other option accepted by {GPGME::Ctx.new}

@example

GPGME::Key.import(File.open("my.key"))
# File lib/gpgme/key.rb, line 119
def import(keydata, options = {})
  GPGME::Ctx.new(options) do |ctx|
    ctx.import_keys(Data.new(keydata))
    ctx.import_result
  end
end

Public Instance Methods

==(another_key) click to toggle source
# File lib/gpgme/key.rb, line 212
def ==(another_key)
  self.class === another_key and fingerprint == another_key.fingerprint
end
comment() click to toggle source

Returns the issuer comment for this key.

# File lib/gpgme/key.rb, line 208
def comment
  primary_uid.comment
end
delete!(allow_secret = false) click to toggle source

Delete this key. If it's public, and has a secret one it will fail unless allow_secret is specified as true.

# File lib/gpgme/key.rb, line 148
def delete!(allow_secret = false)
  GPGME::Ctx.new do |ctx|
    ctx.delete_key self, allow_secret
  end
end
email() click to toggle source

Returns the email for this key.

# File lib/gpgme/key.rb, line 196
def email
  primary_uid.email
end
expired() click to toggle source

Returns true if the key is expired

# File lib/gpgme/key.rb, line 168
def expired
  subkeys.any?(&:expired)
end
expires() click to toggle source

Returns the expiry date for this key

# File lib/gpgme/key.rb, line 162
def expires
  primary_subkey.expires
end
expires?() click to toggle source

Returns true if the key has an expiry date else false

# File lib/gpgme/key.rb, line 156
def expires?
  primary_subkey.expires?
end
export(options = {}) click to toggle source

Exports this key. Accepts the same options as {GPGME::Ctx.new}, and +options+, where you can specify something that can become a {GPGME::Data}, where the output will go.

@example

key.export(:armor => true)
# => GPGME::Data you can read with ASCII armored format

@example

file = File.open("key.asc", "w+")
key.export(:output => file)
# => the key will be written to the file.
# File lib/gpgme/key.rb, line 141
def export(options = {})
  Key.export self.sha, options
end
fingerprint() click to toggle source

Longer descriptive value. Can be used to identify the key.

# File lib/gpgme/key.rb, line 184
def fingerprint
  primary_subkey.fingerprint
end
inspect() click to toggle source
# File lib/gpgme/key.rb, line 216
    def inspect
      sprintf("#<#{self.class} %s %4d%s/%s %s trust=%s, owner_trust=%s, \
capability=%s, subkeys=%s, uids=%s>",
              primary_subkey.secret? ? 'sec' : 'pub',
              primary_subkey.length,
              primary_subkey.pubkey_algo_letter,
              primary_subkey.fingerprint[-8 .. -1],
              primary_subkey.timestamp.strftime('%Y-%m-%d'),
              trust.inspect,
              VALIDITY_NAMES[@owner_trust].inspect,
              capability.inspect,
              subkeys.inspect,
              uids.inspect)
    end
name() click to toggle source

Returns the issuer name for this key.

# File lib/gpgme/key.rb, line 202
def name
  primary_uid.name
end
primary_subkey() click to toggle source
# File lib/gpgme/key.rb, line 172
def primary_subkey
  @primary_subkey ||= subkeys.first
end
primary_uid() click to toggle source

Returns the main {GPGME::UserID} for this key.

# File lib/gpgme/key.rb, line 190
def primary_uid
  uids.first
end
sha() click to toggle source

Short descriptive value. Can be used to identify the key.

# File lib/gpgme/key.rb, line 178
def sha
  primary_subkey.sha
end
to_s() click to toggle source
# File lib/gpgme/key.rb, line 231
def to_s
  primary_subkey = subkeys[0]
  s = sprintf("%s   %4d%s/%s %s\n",
              primary_subkey.secret? ? 'sec' : 'pub',
              primary_subkey.length,
              primary_subkey.pubkey_algo_letter,
              primary_subkey.fingerprint[-8 .. -1],
              primary_subkey.timestamp.strftime('%Y-%m-%d'))
  uids.each do |user_id|
    s << "uid\t\t#{user_id.name} <#{user_id.email}>\n"
  end
  subkeys.each do |subkey|
    s << subkey.to_s
  end
  s
end