Class: EzCrypto::Signer
- Defined in:
- lib/extensions/ezcrypto/ezcrypto/ezsig.rb
Overview
The signer is used for signing stuff. It encapsulates the functionality of a private key.
Class Method Summary collapse
-
.decode(encoded, password = nil) ⇒ Object
Decode a PEM encoded Private Key and return a signer.
-
.from_file(filename, password = nil) ⇒ Object
Decode a PEM encoded Private Key file and return a signer.
-
.generate(strength = 2048, type = :rsa) ⇒ Object
Generate a new keypair.
Instance Method Summary collapse
-
#dsa? ⇒ Boolean
Returns true if it is a DSA private key.
-
#initialize(priv, options = {}) ⇒ Signer
constructor
Initialize a Signer with a OpenSSL Private Key.
-
#private_key ⇒ Object
Returns the OpenSSL Private Key object.
-
#public_key ⇒ Object
Returns the OpenSSL Public Key object.
-
#rsa? ⇒ Boolean
Returns true if it is a RSA private key.
-
#sign(data) ⇒ Object
signs data using the private key and the corresponding digest function.
-
#verifier ⇒ Object
Returns the corresponding Verifier object.
Constructor Details
#initialize(priv, options = {}) ⇒ Signer
Initialize a Signer with a OpenSSL Private Key. You generally should not call new directly.
Unless you are interfacing with your own underlying OpenSSL code.
36 37 38 |
# File 'lib/extensions/ezcrypto/ezcrypto/ezsig.rb', line 36 def initialize(priv, = {}) @priv=priv end |
Class Method Details
.decode(encoded, password = nil) ⇒ Object
Decode a PEM encoded Private Key and return a signer. Takes an optional password
56 57 58 59 60 61 62 |
# File 'lib/extensions/ezcrypto/ezcrypto/ezsig.rb', line 56 def self.decode(encoded,password=nil) begin EzCrypto::Signer.new(OpenSSL::PKey::RSA.new( encoded,password)) rescue EzCrypto::Signer.new(OpenSSL::PKey::DSA.new( encoded,password)) end end |
.from_file(filename, password = nil) ⇒ Object
Decode a PEM encoded Private Key file and return a signer. Takes an optional password
67 68 69 70 |
# File 'lib/extensions/ezcrypto/ezcrypto/ezsig.rb', line 67 def self.from_file(filename,password=nil) file = File.read( filename ) decode(file,password) end |
.generate(strength = 2048, type = :rsa) ⇒ Object
Generate a new keypair. Defaults to 2048 bit RSA.
43 44 45 46 47 48 49 50 51 |
# File 'lib/extensions/ezcrypto/ezcrypto/ezsig.rb', line 43 def self.generate(strength=2048,type=:rsa) key_class=case type when :dsa OpenSSL::PKey::DSA else OpenSSL::PKey::RSA end EzCrypto::Signer.new(key_class.generate(strength)) end |
Instance Method Details
#dsa? ⇒ Boolean
Returns true if it is a DSA private key
116 117 118 |
# File 'lib/extensions/ezcrypto/ezcrypto/ezsig.rb', line 116 def dsa? @priv.is_a? OpenSSL::PKey::DSA end |
#private_key ⇒ Object
Returns the OpenSSL Private Key object. You normally do not need to use this.
89 90 91 |
# File 'lib/extensions/ezcrypto/ezcrypto/ezsig.rb', line 89 def private_key @priv end |
#public_key ⇒ Object
Returns the OpenSSL Public Key object. You normally do not need to use this.
75 76 77 |
# File 'lib/extensions/ezcrypto/ezcrypto/ezsig.rb', line 75 def public_key @priv.public_key end |
#rsa? ⇒ Boolean
Returns true if it is a RSA private key
109 110 111 |
# File 'lib/extensions/ezcrypto/ezcrypto/ezsig.rb', line 109 def rsa? @priv.is_a? OpenSSL::PKey::RSA end |
#sign(data) ⇒ Object
signs data using the private key and the corresponding digest function. SHA1 for RSA and DSS1 for DSA.
99% of signing use these parameters.
Email a request or send me a patch if you have other requirements.
98 99 100 101 102 103 104 |
# File 'lib/extensions/ezcrypto/ezcrypto/ezsig.rb', line 98 def sign(data) if rsa? @priv.sign(OpenSSL::Digest::SHA1.new,data) elsif dsa? @priv.sign(OpenSSL::Digest::DSS1.new,data) end end |