Skip to content

infinity.rsa

Provides RSA key generation, encryption, decryption, signing and verification functionality for strings.

Keys are PEM-encoded strings: public keys in the X.509 SubjectPublicKeyInfo format (-----BEGIN PUBLIC KEY-----), private keys in the unencrypted PKCS#8 format (-----BEGIN PRIVATE KEY-----). Keys created by other tools (e.g. OpenSSL) can be used as well; private keys may also be in the traditional unencrypted PKCS#1 format (-----BEGIN RSA PRIVATE KEY-----), public keys must be in the SubjectPublicKeyInfo format. Strings to be encrypted, signed or verified are encoded as UTF-8; encrypted data and signatures are returned as Base64 strings (no line breaks).

For signatures, hashAndSign() and hashAndVerify() should be preferred: they create standard RSASSA-PKCS1-v1_5 signatures of any data length (e.g. SHA256withRSA with infinity.crypto.algorithm.sha256).

Module: infinity.crypto

Example:

infinity.loadModule('infinity.crypto');
let data = 'Hello World';
let keys = infinity.rsa.generateKeys(2048);
let encrypted = infinity.rsa.encrypt(data, keys.public);
console.debug(encrypted);
//Base64 string (344 characters for a 2048-bit key, differs on every call)
console.debug(infinity.rsa.decrypt(encrypted, keys.private));
//Hello World

let signature = infinity.rsa.hashAndSign(data, keys.private, infinity.crypto.algorithm.sha256);
console.debug(infinity.rsa.hashAndVerify(data, keys.public, signature, infinity.crypto.algorithm.sha256));
//true

Interfaces

keys

Properties:


Properties


private

Type: string

Contains the private RSA key (PEM, unencrypted PKCS#8, -----BEGIN PRIVATE KEY-----).


public

Type: string

Contains the public RSA key (PEM, X.509 SubjectPublicKeyInfo, -----BEGIN PUBLIC KEY-----).


Functions

decrypt()

Decrypts the given Base64 string (as returned by encrypt()) with RSA (OAEP padding) using the provided private key and returns the decrypted data, decoded as UTF-8. Returns an empty string if value is empty. Throws an exception if the private key cannot be loaded or the data cannot be decrypted with it (before 3.11: an empty string).

Signature:

decrypt( value: string, privateKey: string ): string

Parameters:

  • value: string

    The Base64-encoded encrypted data.

  • privateKey: string

    The private RSA key (PEM).

Return type: string

Example:

infinity.loadModule('infinity.crypto');
let decryptedData = infinity.rsa.decrypt(encryptedData, privateKey);

encrypt()

Encrypts the given string (encoded as UTF-8) with RSA using the provided public key and returns the encrypted data as a Base64 string. OAEP padding (RSAES-OAEP with SHA-1 and MGF1-SHA-1, the OpenSSL default) is used, so the result differs on every call. Returns an empty string if value is empty. Throws an exception if the public key cannot be loaded or the data is too long for the key (before 3.11: a useless result).

RSA can only encrypt small amounts of data: the UTF-8 encoded value must not be longer than the key size in bytes minus 42 (e.g. 214 bytes for a 2048-bit key). To encrypt larger data, encrypt a random password with RSA and the data itself with infinity.aes256.

Signature:

encrypt( value: string, publicKey: string ): string

Parameters:

  • value: string

    The data to encrypt.

  • publicKey: string

    The public RSA key (PEM) of the recipient. The data can only be decrypted with the corresponding private key.

Return type: string

Example:

infinity.loadModule('infinity.crypto');
let encryptedData = infinity.rsa.encrypt(dataToEncrypt, publicKey);

generateKeys()

Generates a new RSA key pair with the specified modulus bit length and returns the public and the private key as PEM strings. Sizes below 512 bits give a 2048-bit key (the OpenSSL default). Throws an exception if OpenSSL cannot generate the key. Large sizes take long (several seconds for 4096 bits).

Signature:

generateKeys( bits: number ): infinity.rsa.keys

Parameters:

  • bits: number

    The number of bits in the modulus, e.g. 2048 or 4096. Use at least 2048.

Return type: infinity.rsa.keys

Example:

infinity.loadModule('infinity.crypto');
let keys = infinity.rsa.generateKeys(2048);
console.debug(keys.private);
console.debug(keys.public);

hashAndSign()

Hashes the given string (encoded as UTF-8) with the specified algorithm and returns the RSA signature (RSASSA-PKCS1-v1_5) of the hash, created with the provided private key, as a Base64 string. Returns an empty string if value is empty (there is no signature of empty data). Throws an exception if the private key cannot be loaded.

Signature:

hashAndSign( value: string, privateKey: string, algorithm?: infinity.crypto.algorithm ): string

Parameters:

  • value: string

    The string to create the signature for.

  • privateKey: string

    The private RSA key (PEM).

  • algorithm: infinity.crypto.algorithm, optional

    The hash algorithm. Defaults to infinity.crypto.algorithm.sha1; infinity.crypto.algorithm.sha256 or higher is recommended.

Return type: string

Example:

infinity.loadModule('infinity.crypto');
let signature = infinity.rsa.hashAndSign(dataToSign, privateKey, infinity.crypto.algorithm.sha256);

hashAndVerify()

Returns whether the given signature (as created by hashAndSign()) is a valid RSA signature (RSASSA-PKCS1-v1_5) of the given string for the provided public key. The same algorithm as for signing must be used. Returns false for an empty value or signature (before 3.11: true for an empty value, whatever the signature and key). Throws an exception if the public key cannot be loaded.

Signature:

hashAndVerify( value: string, publicKey: string, signature: string, algorithm?: infinity.crypto.algorithm ): boolean

Parameters:

  • value: string

    The signed data.

  • publicKey: string

    The public RSA key (PEM).

  • signature: string

    The Base64-encoded signature.

  • algorithm: infinity.crypto.algorithm, optional

    The hash algorithm used for signing. Defaults to infinity.crypto.algorithm.sha1.

Return type: boolean

Example:

infinity.loadModule('infinity.crypto');
if (infinity.rsa.hashAndVerify(dataToVerify, publicKey, signature, infinity.crypto.algorithm.sha256)) {
  //...
}

sign()

Returns the RSA signature of the given string (encoded as UTF-8), created with the provided private key, as a Base64 string. The data is signed directly with PKCS#1 v1.5 padding, without hashing it first; therefore it must not be longer than the key size in bytes minus 11 (e.g. 245 bytes for a 2048-bit key). For data of any length and for signatures compatible with other systems, use hashAndSign(). Returns an empty string if value is empty (there is no signature of empty data). Throws an exception if the private key cannot be loaded or the data is too long for the key (before 3.11: a useless result).

Signature:

sign( value: string, privateKey: string ): string

Parameters:

  • value: string

    The string to create the signature for.

  • privateKey: string

    The private RSA key (PEM).

Return type: string

Example:

infinity.loadModule('infinity.crypto');
let signature = infinity.rsa.sign(dataToSign, privateKey);

verify()

Returns whether the given signature (as created by sign()) is a valid RSA signature of the given string for the provided public key. Returns false for an empty value or signature (before 3.11: true for an empty value, whatever the signature and key). Throws an exception if the public key cannot be loaded.

Signature:

verify( value: string, publicKey: string, signature: string ): boolean

Parameters:

  • value: string

    The signed data.

  • publicKey: string

    The public RSA key (PEM).

  • signature: string

    The Base64-encoded signature.

Return type: boolean

Example:

infinity.loadModule('infinity.crypto');
if ( infinity.rsa.verify(dataToVerify, publicKey, signature) ) {
  //...
}