Class: EzCrypto::Verifier

### Home Classes Methods

### In Files

- lib/ezsig.rb

### Parent

Object

### Methods

- ::decode
- ::from_file
- ::from_pkyp
- ::load_all_from_file
- ::new
- #cert?
- #digest
- #dsa?
- #public_key
- #register_with_pkyp
- #rsa?
- #send_to_pkyp
- #verify

### Files

- CHANGELOG
- README.rdoc
- README_ACTIVE_CRYPTO
- README_DIGITAL_SIGNATURES

### Class/Module Index

Quicksearch

- EzCrypto
- EzCrypto::Certificate
- EzCrypto::Digester
- EzCrypto::Key
- EzCrypto::Name
- EzCrypto::Signer
- EzCrypto::TrustStore
- EzCrypto::Verifier
- ActiveCrypto
- ActiveCrypto::AssociationKeyHolder
- ActiveCrypto::ClassMethods
- ActiveCrypto::Encrypted
- ActiveCrypto::Encrypted::ClassAccessors
- ActiveCrypto::KeyHolder
- ActiveCrypto::MissingKeyError

No matching classes.

# EzCrypto::Verifier

The Verifier is used for verifying signatures. If you use the decode or from_file methods you can use either raw PEM encoded public keys or certificate.

### Public Class Methods

decode(encoded) click to toggle source

Decodes a PEM encoded Certificate or Public Key and returns a Verifier object.

    # File lib/ezsig.rb, line 137
    def self.decode(encoded)
      case encoded
      when /-----BEGIN CERTIFICATE-----/
        EzCrypto::Certificate.new(OpenSSL::X509::Certificate.new( encoded))
      else
        begin
          EzCrypto::Verifier.new(OpenSSL::PKey::RSA.new( encoded))
        rescue
          EzCrypto::Verifier.new(OpenSSL::PKey::DSA.new( encoded))
        end
      end
    end

from_file(filename) click to toggle source

Decodes a PEM encoded Certificate or Public Key from a file and returns a Verifier object.

    # File lib/ezsig.rb, line 153
    def self.from_file(filename)
      file = File.read( filename )
      decode(file)
    end

from_pkyp(digest) click to toggle source

Load a certificate or public key from PKYP based on it's hex digest

    # File lib/ezsig.rb, line 161
        def self.from_pkyp(digest)
          digest=digest.strip.downcase
          if digest=~/[0123456789abcdef]{40}/
    #        Net::HTTP.start("localhost", 9000) do |query|
            Net::HTTP.start("pkyp.org", 80) do |query|
              response=query.get "/#{digest}.pem"
              if response.code=="200"
                decode(response.body)
              else
                raise "Error occured (#{response.code}): #{response.body}"
              end
            end
          else
            raise "Invalid digest"
          end
        end

load_all_from_file(filename) click to toggle source

Decodes all certificates or public keys in a file and returns an array.

    # File lib/ezsig.rb, line 181
    def self.load_all_from_file(filename)
      file = File.read( filename )
      certs=[]
      count=0
      file.split( %{-----BEGIN}).each do |pem|
        if pem and pem!=""
            pem="-----BEGIN#{pem}\n"
              cert=decode(pem)
              if cert.is_a? EzCrypto::Verifier
                certs<<cert
              end
        end
      end
      certs
    end

new(pub) click to toggle source

Initializes a Verifier using a OpenSSL public key object.

    # File lib/ezsig.rb, line 130
    def initialize(pub)
      @pub=pub
    end

### Public Instance Methods

cert?() click to toggle source

Is the Verifier a Certificate or not.

    # File lib/ezsig.rb, line 200
    def cert?
      false
    end

digest() click to toggle source

Returns the SHA1 hexdigest of the DER encoded public key. This can be used as a unique key identifier.

    # File lib/ezsig.rb, line 214
    def digest
      Digest::SHA1.hexdigest(@pub.to_der)
    end

dsa?() click to toggle source

Is this a DSA key?

    # File lib/ezsig.rb, line 226
    def dsa?
      @pub.is_a? OpenSSL::PKey::DSA
    end

public_key() click to toggle source

Returns the OpenSSL public key object. You would normally not need to use this.

    # File lib/ezsig.rb, line 207
    def public_key
      @pub
    end

register_with_pkyp() click to toggle source

Register the public key or certificate at PKYP

    # File lib/ezsig.rb, line 247
    def register_with_pkyp
      send_to_pkyp(@pub.to_s)
    end

rsa?() click to toggle source

Is this a RSA key?

    # File lib/ezsig.rb, line 220
    def rsa?
      @pub.is_a? OpenSSL::PKey::RSA
    end

verify(sig,data) click to toggle source

Returns true if the public key signed the given data.

    # File lib/ezsig.rb, line 234
    def verify(sig,data)
      if rsa?
        @pub.verify( OpenSSL::Digest::SHA1.new, sig, data )
      elsif dsa?
        @pub.verify( OpenSSL::Digest::DSS1.new, sig, data )
      else
        false
      end
    end

### Protected Instance Methods

send_to_pkyp(pem) click to toggle source

    # File lib/ezsig.rb, line 253
        def send_to_pkyp(pem)
    #      Net::HTTP.start("localhost", 9000) do |query|
          Net::HTTP.start("pkyp.org", 80) do |query|
            output=URI.escape(pem).gsub("+","%2b")
            response=query.post "/register","body="+output
            if response.code=="302"
              response["Location"]=~/([0123456789abcdef]{40}$)/
              $1
            else
              raise "Error occured (#{response.code}): #{response.body}"
            end
          end
        end

[Validate]

Generated with the Darkfish Rdoc Generator 2.