class EaSSL::Key

EaSSL::Key creates and manages openSSL keys

Author

Paul Nicholson (paul@webpowerdesign.net)

Co-Author

Adam Williams (adam@thewilliams.ws)

Copyright

Copyright © 2006 WebPower Design

License

Distributes under the same terms as Ruby

Usage

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

Public Class Methods

load(pem_file_path, password=nil) click to toggle source

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

# File lib/eassl/key.rb, line 56
def self.load(pem_file_path, password=nil)
  new.load(File.read(pem_file_path), password)
end
new(options = {}) click to toggle source

Create new Key using the provided options or using the defaults

# File lib/eassl/key.rb, line 19
def initialize(options = {}) #:params: options
  @options = {
    :bits => 2048,
    :password => 'ssl_password',
  }.update(options)
end

Public Instance Methods

length() click to toggle source

Returns the length of the key in bits

# File lib/eassl/key.rb, line 46
def length
  ssl.n.num_bytes * 8
end
load(pem_string, password=nil) click to toggle source

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

# 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_key() click to toggle source
# File lib/eassl/key.rb, line 41
def private_key
  ssl
end
ssl() click to toggle source
# 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_pem() click to toggle source

Export the encrypted key, returns a string

# File lib/eassl/key.rb, line 51
def to_pem
  ssl.export(OpenSSL::Cipher::DES.new('EDE3-CBC'), @options[:password])
end