Class: EzCrypto::Signer
- Inherits:
-
Object
- Object
- EzCrypto::Signer
- Defined in:
- lib/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.
35 36 37 |
# File 'lib/ezsig.rb', line 35 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
55 56 57 58 59 60 61 |
# File 'lib/ezsig.rb', line 55 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
66 67 68 69 |
# File 'lib/ezsig.rb', line 66 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.
42 43 44 45 46 47 48 49 50 |
# File 'lib/ezsig.rb', line 42 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
115 116 117 |
# File 'lib/ezsig.rb', line 115 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.
88 89 90 |
# File 'lib/ezsig.rb', line 88 def private_key @priv end |
#public_key ⇒ Object
Returns the OpenSSL Public Key object. You normally do not need to use this.
74 75 76 |
# File 'lib/ezsig.rb', line 74 def public_key @priv.public_key end |
#rsa? ⇒ Boolean
Returns true if it is a RSA private key
108 109 110 |
# File 'lib/ezsig.rb', line 108 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.
97 98 99 100 101 102 103 |
# File 'lib/ezsig.rb', line 97 def sign(data) if rsa? @priv.sign(OpenSSL::Digest::SHA1.new,data) elsif dsa? @priv.sign(OpenSSL::Digest::DSS1.new,data) end end |