data Crypto::RSA::PrivateKey(der: Bytes)

An RSA private key.

key = Crypto::RSA::PrivateKey.generate(2048)
File.write("key.pem", key.to_pem)
File.write("key.pub.pem", key.public_key.to_pem)

<std:crypto>:296

Class methods

def self.new(der: Bytes) -> Crypto::RSA::PrivateKey

Raises Crypto::Error when der is not an RSA private key (PKCS#8 or PKCS#1); it is kept as PKCS#8.

<std:crypto>:302

def self.generate(bits: Int = …) -> Crypto::RSA::PrivateKey

A new key of bits bits: 2048, 3072 or 4096 (ArgumentError otherwise). It is made on a helper thread: only the calling task waits (hundreds of milliseconds for 4096 bits).

<std:crypto>:309

def self.from_pem(pem: String | Bytes) -> Crypto::RSA::PrivateKey

The key in pem: one PKCS#8 (PRIVATE KEY) or PKCS#1 (RSA PRIVATE KEY) block, alone but for whitespace, not encrypted (Crypto::Error otherwise, as for a key of another kind or size).

pem = File.read("std/test/fixtures/crypto/rsa2048_pkcs1.pem")
Crypto::RSA::PrivateKey.from_pem(pem).bits   # => 2048

<std:crypto>:324

def self.from_der(der: Bytes) -> Crypto::RSA::PrivateKey

The key in der: PKCS#8 or PKCS#1.

<std:crypto>:329

Methods

def der -> Bytes

The key as PKCS#8 DER, whatever it was read from.

<std:crypto>:298

def to_h -> Hash[Symbol, Object]

The fields as a Hash, by name.

<std:crypto>:302

def with(*, der: Bytes = …) -> Crypto::RSA::PrivateKey

A copy with the fields given replaced.

<std:crypto>:302

def to_der -> Bytes

The key as DER: PKCS#8.

<std:crypto>:332

def to_pem(format: Symbol = …) -> String

The key as PEM: PKCS#8 (PRIVATE KEY), or PKCS#1 (RSA PRIVATE KEY) with format: :pkcs1.

<std:crypto>:336

def public_key -> Crypto::RSA::PublicKey

The public half.

<std:crypto>:339

def bits -> Int

The modulus's size in bits.

<std:crypto>:342

def sign(data: Bytes | String, hash: Symbol) -> Bytes

The RSASSA-PKCS1-v1_5 signature of data with hash.

<std:crypto>:345

def sign_pss(data: Bytes | String, hash: Symbol) -> Bytes

The RSASSA-PSS signature of data with hash (MGF1 with the same hash, a salt as long as the hash). Each signature differs: the salt is random.

<std:crypto>:350

def inspect -> String

#<Crypto::RSA::PrivateKey 2048 bits>: never the key.

<std:crypto>:353

def to_s -> String

#<Crypto::RSA::PrivateKey 2048 bits>: never the key.

<std:crypto>:356