RSA is an asymmetric public key algorithm that has been formalized in RFC 3447. It is in widespread use in public key infrastructures (PKI) where certificates (cf. OpenSSL::X509::Certificate) often are issued on the basis of a public/private RSA key pair. RSA is used in a wide field of applications such as secure (symmetric) key exchange, e.g. when establishing a secure TLS/SSL connection. It is also used in various digital signature schemes.

Constants
No documentation available
No documentation available
No documentation available
No documentation available
Class Methods

Generates an RSA keypair.

See also OpenSSL::PKey.generate_key.

size

The desired key size in bits.

exponent

An odd Integer, normally 3, 17, or 65537.

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:

OpenSSL::PKey::RSA.new 2048
OpenSSL::PKey::RSA.new File.read 'rsa.pem'
OpenSSL::PKey::RSA.new File.read('rsa.pem'), 'my pass phrase'
Instance Methods

Outputs this keypair in PEM encoding. If cipher and pass_phrase are given they will be used to encrypt the key. cipher must be an OpenSSL::Cipher instance.

No documentation available

THIS METHOD IS INSECURE, PRIVATE INFORMATION CAN LEAK OUT!!!

Stores all parameters of key to the hash. The hash has keys ‘n’, ‘e’, ‘d’, ‘p’, ‘q’, ‘dmp1’, ‘dmq1’, ‘iqmp’.

Don’t use :-)) (It’s up to you)

Does this keypair contain a private key?

Decrypt string, which has been encrypted with the public key, with the private key. padding defaults to PKCS1_PADDING.

Deprecated in version 3.0. Consider using PKey::PKey#encrypt and PKey::PKey#decrypt instead.

Encrypt string with the private key. padding defaults to PKCS1_PADDING. The encrypted string output can be decrypted using public_decrypt.

Deprecated in version 3.0. Consider using PKey::PKey#sign_raw and PKey::PKey#verify_raw, and PKey::PKey#verify_recover instead.

The return value is always true since every private key is also a public key.

Decrypt string, which has been encrypted with the private key, with the public key. padding defaults to PKCS1_PADDING.

Deprecated in version 3.0. Consider using PKey::PKey#sign_raw and PKey::PKey#verify_raw, and PKey::PKey#verify_recover instead.

Encrypt string with the public key. padding defaults to PKCS1_PADDING. The encrypted string output can be decrypted using private_decrypt.

Deprecated in version 3.0. Consider using PKey::PKey#encrypt and PKey::PKey#decrypt instead.

Returns a new RSA instance that carries just the public key components.

This method is provided for backwards compatibility. In most cases, there is no need to call this method.

For the purpose of serializing the public key, to PEM or DER encoding of X.509 SubjectPublicKeyInfo format, check PKey#public_to_pem and PKey#public_to_der.

Sets dmp1, dmq1, iqmp for the RSA instance. They are calculated by d mod (p - 1), d mod (q - 1) and q^(-1) mod p respectively.

Sets p, q for the RSA instance.

Sets n, e, d for the RSA instance.

Signs data using the Probabilistic Signature Scheme (RSA-PSS) and returns the calculated signature.

RSAError will be raised if an error occurs.

See verify_pss for the verification operation.

Parameters

digest

A String containing the message digest algorithm name.

data

A String. The data to be signed.

salt_length

The length in octets of the salt. Two special values are reserved: :digest means the digest length, and :max means the maximum possible length for the combination of the private key and the selected message digest algorithm.

mgf1_hash

The hash algorithm used in MGF1 (the currently supported mask generation function (MGF)).

Example

data = "Sign me!"
pkey = OpenSSL::PKey::RSA.new(2048)
signature = pkey.sign_pss("SHA256", data, salt_length: :max, mgf1_hash: "SHA256")
pub_key = OpenSSL::PKey.read(pkey.public_to_der)
puts pub_key.verify_pss("SHA256", signature, data,
                        salt_length: :auto, mgf1_hash: "SHA256") # => true

Outputs this keypair in DER encoding.

No documentation available

Verifies data using the Probabilistic Signature Scheme (RSA-PSS).

The return value is true if the signature is valid, false otherwise. RSAError will be raised if an error occurs.

See sign_pss for the signing operation and an example code.

Parameters

digest

A String containing the message digest algorithm name.

data

A String. The data to be signed.

salt_length

The length in octets of the salt. Two special values are reserved: :digest means the digest length, and :auto means automatically determining the length based on the signature.

mgf1_hash

The hash algorithm used in MGF1.