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.



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

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.



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

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

Instance Method Details

#load(pem_string, password = nil) ⇒ Object

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



55
56
57
58
59
60
61
62
# File 'lib/eassl/key.rb', line 55

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



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

def private_key
  ssl
end

#sslObject



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

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

#to_pemObject

Export the encrypted key, returns a string



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

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