Class: EaSSL::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/eassl/key.rb

Overview

EaSSL::Key creates and manages openSSL keys

Author

Paul Nicholson ([email protected])

Co-Author

Adam Williams ([email protected])

Copyright

Copyright © 2006 WebPower Design

License

Distributes under the same terms as Ruby

Usage

Availible Methods - including methods provided by openSSL::PKey:
  • public_key

  • private_key

  • to_text

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Key

Create new Key using the provided options or using the defaults



19
20
21
22
23
24
# File 'lib/eassl/key.rb', line 19

def initialize(options = {}) #:params: options
  @options = {
    :bits => 2048,
    :password => 'ssl_password',
  }.update(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object

This method is used to intercept and pass-thru calls to openSSL methods and instance variables.



37
38
39
# File 'lib/eassl/key.rb', line 37

def method_missing(method) # :nodoc:
  ssl.send(method)
end

Class Method Details

.load(pem_file_path, password = nil) ⇒ Object

Decrypt and load a PEM encoded Key from the file system with the provided password.



56
57
58
# File 'lib/eassl/key.rb', line 56

def self.load(pem_file_path, password=nil)
  new.load(File.read(pem_file_path), password)
end

Instance Method Details

#lengthObject

Returns the length of the key in bits



46
47
48
# File 'lib/eassl/key.rb', line 46

def length
  ssl.n.num_bytes * 8
end

#load(pem_string, password = nil) ⇒ Object

Decrypt and load a PEM encoded Key from provided string with the provided password.



61
62
63
64
65
66
67
68
# File 'lib/eassl/key.rb', line 61

def load(pem_string, password=nil)
  begin
    @ssl = OpenSSL::PKey::RSA::new(pem_string, password || @options[:password])
  rescue
    raise "KeyLoader: Error decrypting key with password"
  end
  self
end

#private_keyObject



41
42
43
# File 'lib/eassl/key.rb', line 41

def private_key
  ssl
end

#sslObject



26
27
28
29
30
31
32
33
# File 'lib/eassl/key.rb', line 26

def ssl
  unless @ssl
    # <Should use some kind of logger on this>
    # $stderr.puts "Generating #{@options[:bits]} bit key\n"
    @ssl = OpenSSL::PKey::RSA::new(@options[:bits])
  end
  @ssl
end

#to_pemObject

Export the encrypted key, returns a string



51
52
53
# File 'lib/eassl/key.rb', line 51

def to_pem
  ssl.export(OpenSSL::Cipher::DES.new('EDE3-CBC'), @options[:password])
end