class OpenSSL::PKey::RSA
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_PADDING
- PKCS1_OAEP_PADDING
- PKCS1_PADDING
- SSLV23_PADDING
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7219
def self.generate: (Integer size, ?Integer exponent) -> instance
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7650
def initialize: () -> void
| (Integer key_size) -> void
| (String encoded_key, ?String pass_phrase) -> void
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. This form is not compatible with OpenSSL 3.0 or later.
If called with a String, tries to parse as DER or PEM encoding of an RSA key. Note that if password is not specified, but the key is encrypted with a password, OpenSSL will prompt for it. See also OpenSSL::PKey.read which can parse keys of any kind.
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 password’
Public Instance Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7221
def d: () -> BN?
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7223
def dmp1: () -> BN?
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7225
def dmq1: () -> BN?
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7227
def e: () -> BN?
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7286
def export: (String cipher, String password) -> String
| () -> String
Serializes a private or public key to a PEM-encoding.
When the key contains public components only : Serializes it into an X.509 SubjectPublicKeyInfo. The parameters cipher and password are ignored.
A PEM-encoded key will look like:
-----BEGIN PUBLIC KEY-----
[...]
-----END PUBLIC KEY-----
Consider using #public_to_pem instead. This serializes the key into an
X.509 SubjectPublicKeyInfo regardless of whether the key is a public key
or a private key.
When the key contains private components, and no parameters are given : Serializes it into a PKCS #1 RSAPrivateKey.
A PEM-encoded key will look like:
-----BEGIN RSA PRIVATE KEY-----
[...]
-----END RSA PRIVATE KEY-----
When the key contains private components, and cipher and password are given : Serializes it into a PKCS #1 RSAPrivateKey and encrypts it in OpenSSL’s traditional PEM encryption format. cipher must be a cipher name understood by OpenSSL::Cipher.new or an instance of OpenSSL::Cipher.
An encrypted PEM-encoded key will look like:
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,733F5302505B34701FC41F5C0746E4C0
[...]
-----END RSA PRIVATE KEY-----
Note that this format uses MD5 to derive the encryption key, and hence
will not be available on FIPS-compliant systems.
<strong>This method is kept for compatibility.</strong> This should only be used when the PKCS #1 RSAPrivateKey format is required.
Consider using public_to_pem (X.509 SubjectPublicKeyInfo) or private_to_pem (PKCS #8 PrivateKeyInfo or EncryptedPrivateKeyInfo) instead.
(self) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7659
def initialize_copy: (self) -> void
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7289
def iqmp: () -> BN?
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7291
def n: () -> BN?
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7293
def p: () -> BN?
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7303
def params: () -> Hash[String, BN?]
Stores all parameters of key to a Hash.
The hash has keys ‘n’, ‘e’, ‘d’, ‘p’, ‘q’, ‘dmp1’, ‘dmq1’, and ‘iqmp’.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7311
def private?: () -> bool
Does this keypair contain a private key?
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7325
def private_decrypt: (String data, ?Integer padding) -> String
Decrypt string, which has been encrypted with the public key, with the private key. padding defaults to PKCS1_PADDING, which is known to be insecure but is kept for backwards compatibility.
<strong>Deprecated in version 3.0</strong>. Consider using PKey::PKey#encrypt and PKey::PKey#decrypt instead.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7339
def private_encrypt: (String data, ?Integer padding) -> String
Encrypt string with the private key. padding defaults to PKCS1_PADDING, which is known to be insecure but is kept for backwards compatibility. The encrypted string output can be decrypted using public_decrypt.
<strong>Deprecated in version 3.0</strong>. Consider using PKey::PKey#sign_raw and PKey::PKey#verify_raw, and PKey::PKey#verify_recover instead.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7348
def public?: () -> bool
The return value is always true since every private key is also a public key.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7362
def public_decrypt: (String data, ?Integer padding) -> String
Decrypt string, which has been encrypted with the private key, with the public key. padding defaults to PKCS1_PADDING which is known to be insecure but is kept for backwards compatibility.
<strong>Deprecated in version 3.0</strong>. Consider using PKey::PKey#sign_raw and PKey::PKey#verify_raw, and PKey::PKey#verify_recover instead.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7376
def public_encrypt: (String data, ?Integer padding) -> String
Encrypt string with the public key. padding defaults to PKCS1_PADDING, which is known to be insecure but is kept for backwards compatibility. The encrypted string output can be decrypted using private_decrypt.
<strong>Deprecated in version 3.0</strong>. Consider using PKey::PKey#encrypt and PKey::PKey#decrypt instead.
() → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7390
def public_key: () -> self
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.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7392
def q: () -> BN?
(bn dmp1, bn dmq1, bn iqmp) → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7402
def set_crt_params: (bn dmp1, bn dmq1, bn iqmp) -> self
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.
(bn p, bn q) → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7410
def set_factors: (bn p, bn q) -> self
Sets p, q for the RSA instance.
(bn n, bn e, bn d) → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7418
def set_key: (bn n, bn e, bn d) -> self
Sets n, e, d for the RSA instance.
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7457
def sign_pss: (String digest, String data, salt_length: :digest | :max | Integer, mgf1_hash: String) -> String
Signs data using the Probabilistic Signature Scheme (RSA-PSS) and returns the calculated signature.
PKeyError will be raised if an error occurs.
See verify_pss for the verification operation.
Parameters
- digest
-
A
Stringcontaining 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:
:digestmeans the digest length, and:maxmeans 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
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7472
def to_der: () -> String
Serializes a private or public key to a DER-encoding.
See to_pem for details.
<strong>This method is kept for compatibility.</strong> This should only be used when the PKCS #1 RSAPrivateKey format is required.
Consider using public_to_der or private_to_der instead.
Serializes a private or public key to a PEM-encoding.
When the key contains public components only : Serializes it into an X.509 SubjectPublicKeyInfo. The parameters cipher and password are ignored.
A PEM-encoded key will look like:
-----BEGIN PUBLIC KEY-----
[...]
-----END PUBLIC KEY-----
Consider using #public_to_pem instead. This serializes the key into an
X.509 SubjectPublicKeyInfo regardless of whether the key is a public key
or a private key.
When the key contains private components, and no parameters are given : Serializes it into a PKCS #1 RSAPrivateKey.
A PEM-encoded key will look like:
-----BEGIN RSA PRIVATE KEY-----
[...]
-----END RSA PRIVATE KEY-----
When the key contains private components, and cipher and password are given : Serializes it into a PKCS #1 RSAPrivateKey and encrypts it in OpenSSL’s traditional PEM encryption format. cipher must be a cipher name understood by OpenSSL::Cipher.new or an instance of OpenSSL::Cipher.
An encrypted PEM-encoded key will look like:
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,733F5302505B34701FC41F5C0746E4C0
[...]
-----END RSA PRIVATE KEY-----
Note that this format uses MD5 to derive the encryption key, and hence
will not be available on FIPS-compliant systems.
<strong>This method is kept for compatibility.</strong> This should only be used when the PKCS #1 RSAPrivateKey format is required.
Consider using public_to_pem (X.509 SubjectPublicKeyInfo) or private_to_pem (PKCS #8 PrivateKeyInfo or EncryptedPrivateKeyInfo) instead.
Serializes a private or public key to a PEM-encoding.
When the key contains public components only : Serializes it into an X.509 SubjectPublicKeyInfo. The parameters cipher and password are ignored.
A PEM-encoded key will look like:
-----BEGIN PUBLIC KEY-----
[...]
-----END PUBLIC KEY-----
Consider using #public_to_pem instead. This serializes the key into an
X.509 SubjectPublicKeyInfo regardless of whether the key is a public key
or a private key.
When the key contains private components, and no parameters are given : Serializes it into a PKCS #1 RSAPrivateKey.
A PEM-encoded key will look like:
-----BEGIN RSA PRIVATE KEY-----
[...]
-----END RSA PRIVATE KEY-----
When the key contains private components, and cipher and password are given : Serializes it into a PKCS #1 RSAPrivateKey and encrypts it in OpenSSL’s traditional PEM encryption format. cipher must be a cipher name understood by OpenSSL::Cipher.new or an instance of OpenSSL::Cipher.
An encrypted PEM-encoded key will look like:
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,733F5302505B34701FC41F5C0746E4C0
[...]
-----END RSA PRIVATE KEY-----
Note that this format uses MD5 to derive the encryption key, and hence
will not be available on FIPS-compliant systems.
<strong>This method is kept for compatibility.</strong> This should only be used when the PKCS #1 RSAPrivateKey format is required.
Consider using public_to_pem (X.509 SubjectPublicKeyInfo) or private_to_pem (PKCS #8 PrivateKeyInfo or EncryptedPrivateKeyInfo) instead.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7592
def to_text: () -> String
THIS METHOD IS INSECURE, PRIVATE INFORMATION CAN LEAK OUT!!!
Dumps all parameters of a keypair to a String
Don’t use :-)) (It’s up to you)
(String digest, String signature, String data, salt_length: :auto | :digest | Integer, mgf1_hash: String) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 7620
def verify_pss: (String digest, String signature, String data, salt_length: :auto | :digest | Integer, mgf1_hash: String) -> bool
Verifies data using the Probabilistic Signature Scheme (RSA-PSS).
The return value is true if the signature is valid, false otherwise. PKeyError will be raised if an error occurs.
See sign_pss for the signing operation and an example code.
Parameters
- digest
-
A
Stringcontaining 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:
:digestmeans the digest length, and:automeans automatically determining the length based on the signature. - mgf1_hash
-
The hash algorithm used in MGF1.