Skip to content

Variable: sign ​

ts
sign: object;

Digital signature functions for generating and verifying signatures.

Type declaration ​

Generate Signature ​

generate() ​

ts
generate(
   data, 
   privateKey, 
algorithm): Promise<string>;

Generates a digital signature for the given data using the provided private key.

Parameters ​
data ​

string

The data to sign.

privateKey ​

string

The PEM-formatted private key to use for signing.

algorithm ​

HashAlgorithm = HashAlgorithm.SHA256

The hash algorithm used for signing. Defaults to SHA256.

Returns ​

Promise<string>

A Promise that resolves to the generated digital signature as a hexadecimal string.

Throws ​

Will throw an error if the input data or private key is empty or whitespace.

Example ​
js
const payload = JSON.stringify({id: 123, nome: "Heliomar", timestamp: Date.now()})
const  { publicKey, privateKey } = await cryptor.keyPair.generate();

const signature = await cryptor.sign.generate(payload, privateKey);
console.log(Buffer.from(signature).toString("base64"));

const isValid = await cryptor.sign.verify(payload, signature, publicKey);
console.log(isValid); // Outputs true

Verify Signature ​

verify() ​

ts
verify(
   data, 
   signature, 
   publicKey, 
algorithm): Promise<boolean>;

Verifies a digital signature against the given data using the public key.

Parameters ​
data ​

string

The data that was originally signed.

signature ​

string

The signature to verify.

publicKey ​

string

The PEM-formatted public key to use for verification.

algorithm ​

HashAlgorithm = HashAlgorithm.SHA256

The hash algorithm used for signing. Defaults to SHA256.

Returns ​

Promise<boolean>

A Promise that resolves to true if the signature is valid, false otherwise.

Throws ​

Will throw an error if the input data, signature, or public key is empty or whitespace.

Example ​
js
const isValid = await cryptor.sign.verify(payload, signature, publicKey);
console.log(isValid); // Outputs true if the signature is valid, otherwise false