Class RsaKey

extends Object

Class summary


SHA-1 -> any
Produces a 20-byte digest.
SHA-256 -> any
Default. Produces a 32-byte digest.
SHA-384 -> any
Produces a 48-byte digest.
SHA-512 -> any
Produces a 64-byte digest.
PADDING-PKCS1-V15 -> any
Padding scheme PKCS#1 v1.5.
PADDING-OAEP-V21 -> any
Padding scheme PKCS#1 v2.1 (OAEP).
parse-private key/Data --password/string= -> RsaKey
Parses a PKCS#1 or PKCS#8 encoded private key and stores it as DER.
parse-public key/Data -> RsaKey
Parses a PKCS#1 or X.509 encoded public key and stores it as DER.
generate --bits/int= -> RsaKeyPair
Generates a new RSA key pair.

Statics

generate --bits/int=2048 -> RsaKeyPair
Generates a new RSA key pair.
The bits must be one of 1024, 2048, 3072, or 4096.
The default is 2048.

Padding scheme PKCS#1 v2.1 (OAEP).

Padding scheme PKCS#1 v1.5.

parse-private key/Data --password/string="" -> RsaKey
Parses a PKCS#1 or PKCS#8 encoded private key and stores it as DER.
The key must be DER or PEM.
The password is optional and used for encrypted keys.

Parses a PKCS#1 or X.509 encoded public key and stores it as DER.
The key must be DER or PEM.

SHA-1 -> any
Produces a 20-byte digest.

SHA-256 -> any
Default. Produces a 32-byte digest.

SHA-384 -> any
Produces a 48-byte digest.

SHA-512 -> any
Produces a 64-byte digest.

Methods

decrypt data/ByteArray --padding/int=PADDING-PKCS1-V15 --hash/int=SHA-256 -> ByteArray
Decrypts the given data using the private key.
The padding must be either PADDING-PKCS1-V15 or PADDING-OAEP-V21.
Default is PADDING-PKCS1-V15.
The hash is used for OAEP padding and defaults to SHA-256.

encrypt data/ByteArray --padding/int=PADDING-PKCS1-V15 --hash/int=SHA-256 -> ByteArray
Encrypts the given data using the public key.
The padding must be either PADDING-PKCS1-V15 or PADDING-OAEP-V21.
Default is PADDING-PKCS1-V15.
The hash is used for OAEP padding and defaults to SHA-256.

operator == other/any -> bool
Whether this object is equal to the other.
By default, identical is used for equality.
Inheritance
Classes overwrite this operator to get an equality specific to their needs. Equality operators often compare the type and field contents. For example:

class Pin:
  number/int

  constructor .number:

  operator == other:
    if other is not Pin: return false
    return number == other.number
A class doesn't have to follow the above format, but it must keep the operator in sync with any hash-code method. That is, if a class has a hash-code member, then the equality and hash-code must agree. If two instances are equal (a == b), then their hash codes must also be equal (a.hash-code == b.hash-code).

sign message/Data --hash/int=SHA-256 -> ByteArray
Signs the message with this private key.
Hashes the message using the specified hash algorithm, and then signs the digest.
The hash must be one of SHA-1, SHA-256, SHA-384, or SHA-512.
The default is SHA-256.
Examples

signature := key.sign "my message"

Signs the digest with this private key.
The digest must be the hash of the message to sign.
The hash is used to verify that the digest has the correct length.

The size of the key DER in bytes.

Stringifies this object.
Inheritance
Objects that need a human-friendly string representation should overwrite this method. The default string is based on the internal class-ID.

Exports this key in PEM format.
The format depends on whether this is a private or public key:
  • Private key → "-----BEGIN RSA PRIVATE KEY-----"
  • Public key → "-----BEGIN PUBLIC KEY-----"

verify message/Data signature/ByteArray --hash/int=SHA-256 -> bool
Verifies the signature of the message with this public key.
Hashes the message using the specified hash algorithm, and then verifies the signature against the digest.
The hash must be one of SHA-1, SHA-256, SHA-384, or SHA-512.
The default is SHA-256.
Returns true if the signature is valid, false otherwise.
Examples

is-valid := key.verify "my message" signature

verify-digest digest/ByteArray signature/ByteArray --hash/int -> bool
Verifies the signature of the digest with this public key.
The digest must be the hash of the message that was signed.
The hash is used to verify that the digest has the correct length.
Returns true if the signature is valid, false otherwise.

Fields

The DER-encoded key bytes.
For a private key this is a PKCS#1 RSAPrivateKey (or PKCS#8 PrivateKeyInfo)
DER blob. For a public key this is a SubjectPublicKeyInfo DER blob.