class GPGME::Crypto


encrypted = crypto.encrypt ‘Plain text’
crypto = GPGME::Crypto.new :armor => true
@example
format, for example).
know how to customize things further (like output stuff in ASCII armored
common options as {GPGME::Ctx.new}. Read the documentation for that class to
execute crypto operations via GPG. All methods accept as options the same
Different, independent methods providing the simplest possible API to
#

def self.method_missing(method, *args, &block)

create a new instance.
Allows calling of methods directly in the module without the need to
#
def self.method_missing(method, *args, &block)
  if GPGME::Crypto.instance_methods(false).include?(method)
    crypto = GPGME::Crypto.new
    crypto.send method, *args, &block
  else
    super
  end
end

def clearsign(text, options = {})


Same functionality of {.sign} only doing clearsigns by default.

crypto.clearsign text, options

Clearsigns an element
def clearsign(text, options = {})
  sign text, options.merge(:mode => GPGME::SIG_MODE_CLEAR)
end

def decrypt(cipher, options = {})

Raises:
  • (GPGME::Error::DecryptFailed) - when the cipher was encrypted
  • (GPGME::Error::WrongKeyUsage) - TODO Don't know when
  • (GPGME::Error::UnsupportedAlgorithm) - when the cipher was encrypted

Other tags:
    Example: Verifying signatures -
    Example: Output to file -
    Example: symmetric encryption, or passwored key -
    Example: Simple decrypt -

Returns:
  • (GPGME::Data) - a {GPGME::Data} that can be read.

Parameters:
  • &block () --
  • options (Hash) --
  • cipher () --
def decrypt(cipher, options = {})
  options = @default_options.merge options
  plain_data   = Data.new(options[:output])
  cipher_data  = Data.new(cipher)
  GPGME::Ctx.new(options) do |ctx|
    begin
      ctx.decrypt_verify(cipher_data, plain_data)
    rescue GPGME::Error::UnsupportedAlgorithm => exc
      exc.algorithm = ctx.decrypt_result.unsupported_algorithm
      raise exc
    rescue GPGME::Error::WrongKeyUsage => exc
      exc.key_usage = ctx.decrypt_result.wrong_key_usage
      raise exc
    end
    verify_result = ctx.verify_result
    if verify_result && block_given?
      verify_result.signatures.each do |signature|
        yield signature
      end
    end
  end
  plain_data.seek(0)
  plain_data
end

def detach_sign(text, options = {})


Same functionality of {.sign} only doing detached signs by default.

crypto.detach_sign text, options

Creates a detached signature of an element
def detach_sign(text, options = {})
  sign text, options.merge(:mode => GPGME::SIG_MODE_DETACH)
end

def encrypt(plain, options = {})

Raises:
  • (GPGME::Error::General) - when trying to encrypt with a key that is

Other tags:
    Example: writing to a file instead -
    Example: multiple signers -
    Example: encrypted string that can be decrypted and/or *verified* -
    Example: If I didn't trust any of my keys by default -
    Example: to be decrypted by someone@example.com. -
    Example: returns a {GPGME::Data} that can be later encrypted -

Returns:
  • (GPGME::Data) - a {GPGME::Data} object that can be read.

Parameters:
  • options (Hash) --
  • plain () --
def encrypt(plain, options = {})
  options = @default_options.merge options
  plain_data  = Data.new(plain)
  cipher_data = Data.new(options[:output])
  keys        = Key.find(:public, options[:recipients])
  keys        = nil if options[:symmetric]
  flags = 0
  flags |= GPGME::ENCRYPT_ALWAYS_TRUST if options[:always_trust]
  GPGME::Ctx.new(options) do |ctx|
    begin
      if options[:sign]
        if options[:signers]
          signers = Key.find(:public, options[:signers], :sign)
          ctx.add_signer(*signers)
        end
        ctx.encrypt_sign(keys, plain_data, cipher_data, flags)
      else
        ctx.encrypt(keys, plain_data, cipher_data, flags)
      end
    rescue GPGME::Error::UnusablePublicKey => exc
      exc.keys = ctx.encrypt_result.invalid_recipients
      raise exc
    rescue GPGME::Error::UnusableSecretKey => exc
      exc.keys = ctx.sign_result.invalid_signers
      raise exc
    end
  end
  cipher_data.seek(0)
  cipher_data
end

def initialize(options = {})

def initialize(options = {})
  @default_options = options
end

def sign(text, options = {})

Raises:
  • (GPGME::Error::UnusableSecretKey) - TODO don't know when

Other tags:
    Example: specifying the signer -
    Example: doing a detached signature -
    Example: outputing to a file -
    Example: normal sign -

Returns:
  • (GPGME::Data) - a {GPGME::Data} that can be read.

Parameters:
  • options (Hash) --
  • text () --
def sign(text, options = {})
  options = @default_options.merge options
  plain  = Data.new(text)
  output = Data.new(options[:output])
  mode   = options[:mode] || GPGME::SIG_MODE_NORMAL
  GPGME::Ctx.new(options) do |ctx|
    if options[:signer]
      signers = Key.find(:secret, options[:signer], :sign)
      ctx.add_signer(*signers)
    end
    begin
      ctx.sign(plain, output, mode)
    rescue GPGME::Error::UnusableSecretKey => exc
      exc.keys = ctx.sign_result.invalid_signers
      raise exc
    end
  end
  output.seek(0)
  output
end

def verify(sig, options = {})

Other tags:
    Example: verifying a detached signature -
    Example: saving output to file -
    Example: simple verification -

Returns:
  • (GPGME::Data) - unless the sign is detached, the {GPGME::Data}

Parameters:
  • &block () --
  • options (Hash) --
  • sig () --
def verify(sig, options = {})
  options = @default_options.merge options
  sig         = Data.new(sig)
  signed_text = Data.new(options[:signed_text])
  output      = Data.new(options[:output]) unless options[:signed_text]
  GPGME::Ctx.new(options) do |ctx|
    ctx.verify(sig, signed_text, output)
    ctx.verify_result.signatures.each do |signature|
      yield signature
    end
  end
  if output
    output.seek(0)
    output
  end
end