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.


35
36
37
# File 'lib/ezsig.rb', line 35

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



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

Returns:

  • (Boolean)


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

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.



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

def private_key
  @priv
end

#public_keyObject

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

Returns:

  • (Boolean)


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

#verifierObject

Returns the corresponding Verifier object.



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

def verifier
  Verifier.new(public_key)
end