Cryptography is the process of converting plain text into unreadable which is hashed from text and vice-versa and the crypto.sign() is used to create signature of data.
Syntax:
crypto.sign(algorithm, data, key)
Parameters: This function accepts the following parameters:
- algorithm: It is a string-type value. A signature can be created by applying the name of signature algorithms, like ‘SHA256’, in place of a digest algorithm.
- data: It should be an object of buffer, TypedArray, or DataView. Read Buffer.from() method to convert string to Buffer.
- key: It should be the privateKey of keyObject. If you have not any private keys then you can create private and public key using crypto.generateKeyPairSync() method.
Module Installation: Install the required module using the following command:
npm install crypto
Return Value: It returns the signature value base on the specified algorithm, data & key. The returned value is Buffer by default but possible to convert on other format using buffer.toString() method.
Example 1: Sign a string.
signature.js
const crypto = require( 'crypto' );
const buffer = require( 'buffer' );
const { privateKey, publicKey } = crypto.generateKeyPairSync( 'rsa' , {
modulusLength: 2048,
});
const data = Buffer.from( "I Love GeeksForGeeks" );
const sign = crypto.sign( "SHA256" , data , privateKey);
const signature = sign.toString( 'base64' );
console.log(`Signature:\n\n ${signature}`);
|
Run the signature.js file using the following command:
node signature.js
Output:

Example 2: Sign a file.
signature.js
const crypto = require( 'crypto' );
const buffer = require( 'buffer' );
const fs = require( 'fs' );
const { privateKey } = crypto.generateKeyPairSync( 'rsa' , {
modulusLength: 2048,
});
console.log( "Reading File...\n" );
const text = fs.readFileSync( './doc.txt' );
console.log(`File content: ${text}`);
const data = Buffer.from(text);
const sign = crypto.sign( "SHA256" , data , privateKey);
const signature = sign.toString( 'base64' );
console.log(`Signature:\n\n ${signature}`);
|
Run the signature.js file using the following command:
node signature.js
Output:

Example 3: Sign JSON data.
signature.js
const crypto = require( 'crypto' );
const buffer = require( 'buffer' );
const { privateKey } = crypto.generateKeyPairSync( 'rsa' , {
modulusLength: 2048,
});
const person = {
name: "Raktim Banerjee" ,
email: "example@gmail.com" ,
address: "4 main street"
}
const data = Buffer.from( JSON.stringify(person) );
const sign = crypto.sign( "SHA256" , data , privateKey);
const signature = sign.toString( 'base64' );
console.log(`Signature:\n\n ${signature}`);
|
Run the signature.js file using the following command:
node signature.js
Output:

Reference:https://nodejs.org/api/crypto.html#crypto_crypto_sign_algorithm_data_key_callback
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
24 Mar, 2021
Like Article
Save Article