Class: EzCrypto::Verifier
- Inherits:
-
Object
- Object
- EzCrypto::Verifier
- 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
Class Method Summary collapse
-
.decode(encoded) ⇒ Object
Decodes a PEM encoded Certificate or Public Key and returns a Verifier object.
-
.from_file(filename) ⇒ Object
Decodes a PEM encoded Certificate or Public Key from a file and returns a Verifier object.
-
.load_all_from_file(filename) ⇒ Object
Decodes all certificates or public keys in a file and returns an array.
Instance Method Summary collapse
-
#cert? ⇒ Boolean
Is the Verifier a Certificate or not.
-
#digest ⇒ Object
Returns the SHA1 hexdigest of the DER encoded public key.
-
#dsa? ⇒ Boolean
Is this a DSA key?.
-
#initialize(pub) ⇒ Verifier
constructor
Initializes a Verifier using a OpenSSL public key object.
-
#public_key ⇒ Object
Returns the OpenSSL public key object.
-
#rsa? ⇒ Boolean
Is this a RSA key?.
-
#verify(sig, data) ⇒ Object
Returns true if the public key signed the given data.
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 148 149 150 151 |
# File 'lib/ezsig.rb', line 136 def self.decode(encoded) begin 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 rescue puts encoded end end |
.from_file(filename) ⇒ Object
Decodes a PEM encoded Certificate or Public Key from a file and returns a Verifier object.
156 157 158 159 |
# File 'lib/ezsig.rb', line 156 def self.from_file(filename) file = File.read( filename ) decode(file) end |
.load_all_from_file(filename) ⇒ Object
Decodes all certificates or public keys in a file and returns an array.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/ezsig.rb', line 164 def self.load_all_from_file(filename) file = File.read( filename ) certs=[] count=0 file.split( %q{-----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 |
Instance Method Details
#cert? ⇒ Boolean
Is the Verifier a Certificate or not.
183 184 185 |
# File 'lib/ezsig.rb', line 183 def cert? false end |
#digest ⇒ Object
Returns the SHA1 hexdigest of the DER encoded public key. This can be used as a unique key identifier.
197 198 199 |
# File 'lib/ezsig.rb', line 197 def digest Digest::SHA1.hexdigest(@pub.to_der) end |
#dsa? ⇒ Boolean
Is this a DSA key?
209 210 211 |
# File 'lib/ezsig.rb', line 209 def dsa? @pub.is_a? OpenSSL::PKey::DSA end |
#public_key ⇒ Object
Returns the OpenSSL public key object. You would normally not need to use this.
190 191 192 |
# File 'lib/ezsig.rb', line 190 def public_key @pub end |
#rsa? ⇒ Boolean
Is this a RSA key?
203 204 205 |
# File 'lib/ezsig.rb', line 203 def rsa? @pub.is_a? OpenSSL::PKey::RSA end |
#verify(sig, data) ⇒ Object
Returns true if the public key signed the given data.
217 218 219 220 221 222 223 224 225 |
# File 'lib/ezsig.rb', line 217 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 |