Class: EzCrypto::Verifier

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

Overview

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.

Direct Known Subclasses

Certificate

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pub) ⇒ Verifier

Initializes a Verifier using a OpenSSL public key object.



129
130
131
# File 'lib/ezsig.rb', line 129

def initialize(pub)
  @pub=pub
end

Class Method Details

.decode(encoded) ⇒ Object

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



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ezsig.rb', line 136

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) ⇒ Object

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



152
153
154
155
# File 'lib/ezsig.rb', line 152

def self.from_file(filename)
  file = File.read( filename )
  decode(file)
end

Instance Method Details

#cert?Boolean

Is the Verifier a Certificate or not.

Returns:

  • (Boolean)


160
161
162
# File 'lib/ezsig.rb', line 160

def cert?
  false
end

#digestObject

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



174
175
176
# File 'lib/ezsig.rb', line 174

def digest
  Digest::SHA1.hexdigest(@pub.to_der)
end

#dsa?Boolean

Is this a DSA key?

Returns:

  • (Boolean)


186
187
188
# File 'lib/ezsig.rb', line 186

def dsa?
  @pub.is_a? OpenSSL::PKey::DSA
end

#public_keyObject

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



167
168
169
# File 'lib/ezsig.rb', line 167

def public_key
  @pub
end

#rsa?Boolean

Is this a RSA key?

Returns:

  • (Boolean)


180
181
182
# File 'lib/ezsig.rb', line 180

def rsa?
  @pub.is_a? OpenSSL::PKey::RSA
end

#verify(sig, data) ⇒ Object

Returns true if the public key signed the given data.



194
195
196
197
198
199
200
201
202
# File 'lib/ezsig.rb', line 194

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