class

Beryl::Crypto::V1::RivestShamirAdleman::PublicKey

Inherits Reference < Object

An RSA public key: a modulus of 2,048 to 8,192 bits and an odd public exponent from 3 to 2^32 - 1. It verifies signatures and encrypts.

public_key = Beryl::Crypto::V1::RSA::PublicKey.new(modulus_bytes, 65_537)
public_key.verify_pkcs1_signature(message, signature, hash_function: Beryl::Crypto::V1::SHA256) # => true or false

Everything a public key does is public, so its operations may take time that depends on their inputs.

Constants

LARGEST_MODULUS_BIT_COUNT = 8192
LARGEST_PUBLIC_EXPONENT = 4294967295_u64
SMALLEST_MODULUS_BIT_COUNT = 2048
SMALLEST_PUBLIC_EXPONENT = 3_u64

Constructors

new(modulus : Bytes, public_exponent : Bytes | Int)

Takes the big-endian modulus (leading zero bytes allowed) and the public exponent, as big-endian bytes or as an integer. Raises KeySizeNotSupported or PublicExponentNotSupported when either is outside the ranges above, or when the modulus is even.

Source

Instance methods

encrypt_with_oaep(message : Bytes | String, *, hash_function : D.class, label : Bytes | String = Bytes.empty) : Bytes forall D

Returns message encrypted with RSAES-OAEP (RFC 8017 §7.1.1), with hash_function for the label and for MGF1 (Compatibility::SHA1 or a SHA-2 hash) and an optional label. Raises MessageTooLongForModulus when the message is longer than the modulus size less twice the hash size less 2 bytes.

Source
modulus_bit_count

The size of the modulus in bits.

Source
modulus_bytesize

The size of a signature or a ciphertext under this key, in bytes.

Source
public_exponent

The public exponent.

Source
verify_pkcs1_signature(message : Bytes | String, signature : Bytes, *, hash_function : D.class) : Bool forall D

Returns whether signature is a PKCS #1 v1.5 signature (RFC 8017 §8.2) of message hashed with hash_function: one of Compatibility::SHA1, SHA224, SHA256, SHA384, SHA512, SHA512_256. The expected encoding is built and compared whole, so only the one exact encoding is accepted. Never raises for a malformed signature.

Source
verify_pss_signature(message : Bytes | String, signature : Bytes, *, hash_function : D.class, salt_bytesize : Int32) : Bool forall D

Returns whether signature is a PSS signature (RFC 8017 §8.1) of message, with hash_function (a SHA-2 hash) for the message and for MGF1, and a salt of salt_bytesize bytes. Never raises for a malformed signature.

Source