Class: EzCrypto::Signer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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/ezsig.rb', line 36

def initialize(priv,options = {})
  @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/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/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/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

Returns:

  • (Boolean)


116
117
118
# File 'lib/ezsig.rb', line 116

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

#private_keyObject

Returns the OpenSSL Private Key object. You normally do not need to use this.



89
90
91
# File 'lib/ezsig.rb', line 89

def private_key
  @priv
end

#public_keyObject

Returns the OpenSSL Public Key object. You normally do not need to use this.



75
76
77
# File 'lib/ezsig.rb', line 75

def public_key
  @priv.public_key
end

#rsa?Boolean

Returns true if it is a RSA private key

Returns:

  • (Boolean)


109
110
111
# File 'lib/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/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

#verifierObject

Returns the corresponding Verifier object.



82
83
84
# File 'lib/ezsig.rb', line 82

def verifier
  Verifier.new(public_key)
end