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)
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.
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).
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
def self.from_der(der: Bytes) -> Crypto::RSA::PrivateKey
The key in der: PKCS#8 or PKCS#1.
Methods
def der -> Bytes
The key as PKCS#8 DER, whatever it was read from.
def to_h -> Hash[Symbol, Object]
The fields as a Hash, by name.
def with(*, der: Bytes = …) -> Crypto::RSA::PrivateKey
A copy with the fields given replaced.
def to_der -> Bytes
The key as DER: PKCS#8.
def to_pem(format: Symbol = …) -> String
The key as PEM: PKCS#8 (PRIVATE KEY), or PKCS#1
(RSA PRIVATE KEY) with format: :pkcs1.
def public_key -> Crypto::RSA::PublicKey
The public half.
def bits -> Int
The modulus's size in bits.
def sign(data: Bytes | String, hash: Symbol) -> Bytes
The RSASSA-PKCS1-v1_5 signature of data with hash.
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.
def inspect -> String
#<Crypto::RSA::PrivateKey 2048 bits>: never the key.
def to_s -> String
#<Crypto::RSA::PrivateKey 2048 bits>: never the key.