зеркало из https://github.com/github/ruby-gpgme.git
GPGME.import/export moved from HighLevel to GPGME::Key
This commit is contained in:
Родитель
59869c326b
Коммит
0440cc295a
|
@ -34,6 +34,7 @@ module GPGME
|
|||
# signers. Must be an array of sign identifiers.
|
||||
# * +:output+ if specified, it will write the output into it. It will be
|
||||
# converted to a {GPGME::Data} object, so it could be a file for example.
|
||||
# * Any other option accepted by {GPGME::Ctx.new}
|
||||
#
|
||||
# @return [GPGME::Data] a {GPGME::Data} object that can be read.
|
||||
#
|
||||
|
@ -113,6 +114,7 @@ module GPGME
|
|||
# * +:output+ if specified, it will write the output into it. It will
|
||||
# me converted to a {GPGME::Data} object, so it can also be a file,
|
||||
# for example.
|
||||
# * Any other option accepted by {GPGME::Ctx.new}
|
||||
#
|
||||
# @param &block
|
||||
# In the block all the signatures are yielded, so one could verify them.
|
||||
|
@ -183,30 +185,31 @@ module GPGME
|
|||
#
|
||||
# @param [Hash] options
|
||||
# Optional parameters.
|
||||
# * +:signer+ sign identifier to sign the text with. Will use the first
|
||||
# key it finds if none specified.
|
||||
# * +:output+ if specified, it will write the output into it. It will be
|
||||
# converted to a {GPGME::Data} object, so it could be a file for example.
|
||||
# * +:mode+ Desired type of signature. Options are:
|
||||
# - +GPGME::SIG_MODE_NORMAL+ for a normal signature. The default one if
|
||||
# not specified.
|
||||
# - +GPGME::SIG_MODE_DETACH+ for a detached signature
|
||||
# - +GPGME::SIG_MODE_CLEAR+ for a cleartext signature
|
||||
# * +:signer+ sign identifier to sign the text with. Will use the first
|
||||
# key it finds if none specified.
|
||||
# * +:output+ if specified, it will write the output into it. It will be
|
||||
# converted to a {GPGME::Data} object, so it could be a file for example.
|
||||
# * +:mode+ Desired type of signature. Options are:
|
||||
# - +GPGME::SIG_MODE_NORMAL+ for a normal signature. The default one if
|
||||
# not specified.
|
||||
# - +GPGME::SIG_MODE_DETACH+ for a detached signature
|
||||
# - +GPGME::SIG_MODE_CLEAR+ for a cleartext signature
|
||||
# * Any other option accepted by {GPGME::Ctx.new}
|
||||
#
|
||||
# @return [GPGME::Data] a {GPGME::Data} that can be read.
|
||||
#
|
||||
# @example normal sign
|
||||
# GPGME.sign "Hi there"
|
||||
# GPGME.sign "Hi there"
|
||||
#
|
||||
# @example outputing to a file
|
||||
# file = File.open("text.sign", "w+")
|
||||
# GPGME.sign "Hi there", :options => file
|
||||
# file = File.open("text.sign", "w+")
|
||||
# GPGME.sign "Hi there", :options => file
|
||||
#
|
||||
# @example doing a detached signature
|
||||
# GPGME.sign "Hi there", :mode => GPGME::SIG_MODE_DETACH
|
||||
# GPGME.sign "Hi there", :mode => GPGME::SIG_MODE_DETACH
|
||||
#
|
||||
# @example specifying the signer
|
||||
# GPGME.sign "Hi there", :signer => "mrsimo@example.com"
|
||||
# GPGME.sign "Hi there", :signer => "mrsimo@example.com"
|
||||
#
|
||||
# @raise [GPGME::Error::UnusableSecretKey] TODO don't know
|
||||
def sign(text, options = {})
|
||||
|
@ -249,6 +252,8 @@ module GPGME
|
|||
# for which the signature was created.
|
||||
# * +:output+ where to store the result of the signature. Will be
|
||||
# converted to a {GPGME::Data} object.
|
||||
# * Any other option accepted by {GPGME::Ctx.new}
|
||||
#
|
||||
# @param &block
|
||||
# In the block all the signatures are yielded, so one could verify them.
|
||||
# See examples.
|
||||
|
@ -316,59 +321,6 @@ module GPGME
|
|||
GPGME.sign text, options.merge(:mode => GPGME::SIG_MODE_DETACH)
|
||||
end
|
||||
|
||||
# Exports a key
|
||||
#
|
||||
# GPGME.export pattern, options
|
||||
#
|
||||
# @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
|
||||
#
|
||||
# @return [GPGME::Data] the exported key.
|
||||
#
|
||||
# @example
|
||||
# key = GPGME.export "mrsimo@example.com"
|
||||
#
|
||||
# @example writing to a file
|
||||
# out = File.open("my.key", "w+")
|
||||
# GPGME.export "mrsimo@example.com", :output => out
|
||||
#
|
||||
def export(pattern, options = {})
|
||||
check_version(options)
|
||||
|
||||
output = Data.new(options[:output])
|
||||
|
||||
GPGME::Ctx.new(options) do |ctx|
|
||||
ctx.export_keys(pattern, output)
|
||||
end
|
||||
|
||||
output.seek(0)
|
||||
output
|
||||
end
|
||||
|
||||
# Imports a key
|
||||
#
|
||||
# GPGME.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.
|
||||
#
|
||||
# @example
|
||||
# GPGME.import(File.open("my.key"))
|
||||
#
|
||||
def import(keydata, options = {})
|
||||
check_version(options)
|
||||
|
||||
GPGME::Ctx.new(options) do |ctx|
|
||||
ctx.import_keys(Data.new(keydata))
|
||||
ctx.import_result
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# Verify that the engine implementing the protocol +proto+ is installed in
|
||||
# the system. Can be one of +PROTOCOL_OpenPGP+ or +PROTOCOL_CMS+.
|
||||
|
|
143
lib/gpgme/key.rb
143
lib/gpgme/key.rb
|
@ -11,46 +11,119 @@ module GPGME
|
|||
|
||||
include KeyCommon
|
||||
|
||||
##
|
||||
# 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@exampl.com and are
|
||||
# # capable of signing
|
||||
def self.find(secret, keys_or_names = nil, purposes = [])
|
||||
secret = (secret == :secret)
|
||||
keys_or_names = [""] if keys_or_names.nil? || keys_or_names.empty?
|
||||
keys_or_names = [keys_or_names].flatten
|
||||
purposes = [purposes].flatten.compact.uniq
|
||||
class << self
|
||||
|
||||
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)
|
||||
##
|
||||
# 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@exampl.com and are
|
||||
# # capable of signing
|
||||
def find(secret, keys_or_names = nil, purposes = [])
|
||||
secret = (secret == :secret)
|
||||
keys_or_names = [""] if keys_or_names.nil? || 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
|
||||
keys
|
||||
|
||||
# Exports a key
|
||||
#
|
||||
# GPGME::Key.export pattern, options
|
||||
#
|
||||
# @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
|
||||
#
|
||||
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
|
||||
|
||||
# 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"))
|
||||
#
|
||||
def import(keydata, options = {})
|
||||
GPGME::Ctx.new(options) do |ctx|
|
||||
ctx.import_keys(Data.new(keydata))
|
||||
ctx.import_result
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# Exports this key. Accepts the same options as {GPGME::Ctx.new}, and
|
||||
# +options[:output]+, 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.
|
||||
#
|
||||
def export(options = {})
|
||||
Key.export self, options
|
||||
end
|
||||
|
||||
##
|
||||
|
|
|
@ -239,7 +239,7 @@ RUBY
|
|||
GPGME::Key.find(:public).each{|k| k.delete!(true)}
|
||||
assert_equal 0, GPGME::Key.find(:public).size
|
||||
|
||||
result = GPGME.import(export)
|
||||
result = GPGME::Key.import(export)
|
||||
current_keys = GPGME::Key.find(:public)
|
||||
assert_equal original_keys.size, current_keys.size
|
||||
assert_equal result.imports.size, current_keys.size
|
||||
|
@ -261,7 +261,7 @@ RUBY
|
|||
|
||||
key.delete!(true)
|
||||
|
||||
result = GPGME.import(export)
|
||||
result = GPGME::Key.import(export)
|
||||
assert_equal 1, result.imports.size
|
||||
|
||||
import = result.imports.first
|
||||
|
|
|
@ -47,6 +47,58 @@ describe GPGME::Key do
|
|||
end
|
||||
end
|
||||
|
||||
describe :export do
|
||||
# Testing the lazy way with expectations. I think tests in
|
||||
# the Ctx class are enough.
|
||||
it "exports any key that matches the pattern" do
|
||||
GPGME::Ctx.any_instance.expects(:export_keys).with("", anything)
|
||||
GPGME::Key.export("")
|
||||
end
|
||||
|
||||
it "exports any key that matches the pattern, can specify output" do
|
||||
data = GPGME::Data.new
|
||||
GPGME::Ctx.any_instance.expects(:export_keys).with("wadus", data)
|
||||
ret = GPGME::Key.export("wadus", :output => data)
|
||||
assert_equal data, ret
|
||||
end
|
||||
|
||||
it "can specify options for Ctx" do
|
||||
GPGME::Ctx.expects(:new).with(:armor => true).yields(mock(:export_keys => true))
|
||||
GPGME::Key.export("wadus", :armor => true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#export" do
|
||||
it "can export from the key instance" do
|
||||
key = GPGME::Key.find(:public).first
|
||||
GPGME::Key.expects(:export).with(key, {})
|
||||
|
||||
key.export
|
||||
end
|
||||
|
||||
it "can export from the key instance passing variables" do
|
||||
key = GPGME::Key.find(:public).first
|
||||
GPGME::Key.expects(:export).with(key, {:armor => true})
|
||||
|
||||
key.export :armor => true
|
||||
end
|
||||
end
|
||||
|
||||
describe :import do
|
||||
it "can import keys" do
|
||||
data = GPGME::Data.new
|
||||
GPGME::Ctx.any_instance.expects(:import_keys).with(data)
|
||||
GPGME::Ctx.any_instance.expects(:import_result).returns("wadus")
|
||||
|
||||
assert_equal "wadus", GPGME::Key.import(data)
|
||||
end
|
||||
|
||||
it "can specify options for Ctx" do
|
||||
GPGME::Ctx.expects(:new).with(:armor => true).yields(mock(:import_keys => true, :import_result => true))
|
||||
GPGME::Key.import("wadus", :armor => true)
|
||||
end
|
||||
end
|
||||
|
||||
# describe :trust do
|
||||
# it "returns :revoked if it is so"
|
||||
# it "returns :expired if it is expired"
|
||||
|
|
|
@ -16,8 +16,8 @@ def import_keys
|
|||
end
|
||||
|
||||
def import_key(key)
|
||||
GPGME.import key[:public]
|
||||
GPGME.import key[:private]
|
||||
GPGME::Key.import key[:public]
|
||||
GPGME::Key.import key[:private]
|
||||
end
|
||||
|
||||
def remove_keys
|
||||
|
|
Загрузка…
Ссылка в новой задаче