Module: SecurizeString::RSAMethods::ClassMethods

Defined in:
lib/securize_string/rsa_methods.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#rsa_keygen(key_len = 2048, format = :pem) ⇒ Object

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.



28
29
30
31
32
33
# 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