module SecurizeString::RSAMethods::ClassMethods

Adds class methods for OpenSSL::PKey::RSA support via inclusion of SecurizeString::RSAMethods to a class.

Public Instance Methods

rsa_keygen(key_len=2048, format = :pem) click to toggle source

A convenience method for generating random public/private RSA key pairs. Defaults to a key length of 2048, as 1024 is starting to be phased out as the standard for secure communications.

Returns the private key first, then the public key. Returns them in PEM file format by default, as this is most useful for portability. DER format can be explicitly specified with the second argument.

For advanced usage of keys, instantiate an OpenSSL::PKey::RSA object passing the returned key as the argument to new. This will allow introspection of common parameters such as p, q, n, e, and d.

# File lib/securize_string/rsa_methods.rb, line 28
def rsa_keygen(key_len=2048, format = :pem)
  private_key_obj = OpenSSL::PKey::RSA.new(key_len.to_i)
  public_key_obj = private_key_obj.public_key
  formatting_method = (format == :der ? :to_der : :to_pem)
  return [private_key_obj, public_key_obj].map {|k| self.new( k.send(formatting_method) )}
end
separate_keys(pvt_key, format = :pem) click to toggle source

A convenience method for extracting the private, public keypair from a private key.

Returns the same format as rsa_keygen, but takes the private key as a string as a first argument.

# File lib/securize_string/rsa_methods.rb, line 40
def separate_keys(pvt_key, format = :pem)
  private_key_obj = OpenSSL::PKey::RSA.new(pvt_key.to_s)
  public_key_obj = private_key_obj.public_key
  formatting_method = (format == :der ? :to_der : :to_pem)
  return [private_key_obj, public_key_obj].map {|k| self.new( k.send(formatting_method) )}
end