Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class method

OpenSSL::PKey::RSA.new

RSA.new → rsa
RSA.new(encoded_key [, passphrase]) → rsa
RSA.new(encoded_key) { passphrase } → rsa
RSA.new(size [, exponent]) → rsa

Generates or loads an RSA keypair.

If called without arguments, creates a new instance with no key components set. They can be set individually by set_key, set_factors, and set_crt_params.

If called with a String, tries to parse as DER or PEM encoding of an RSA key. Note that, if passphrase is not specified but the key is encrypted with a passphrase, OpenSSL will prompt for it. See also OpenSSL::PKey.read which can parse keys of any kinds.

If called with a number, generates a new key pair. This form works as an alias of RSA.generate.

Examples:

Example
OpenSSL::PKey::RSA.new 2048
OpenSSL::PKey::RSA.new File.read 'rsa.pem'
OpenSSL::PKey::RSA.new File.read('rsa.pem'), 'my pass phrase'
See also .generate #export #initialize_copy also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0