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.
const crypto = require( 'crypto' );
const buffer = require( 'buffer' );
// Create a private key const { privateKey, publicKey } = crypto.generateKeyPairSync( 'rsa' , {
modulusLength: 2048,
}); // Convert string to buffer const data = Buffer.from( "I Love GeeksForGeeks" );
// Sign the data and returned signature in buffer const sign = crypto.sign( "SHA256" , data , privateKey);
// Convert returned buffer to base64 const signature = sign.toString( 'base64' );
// Printing the signature console.log(`Signature:\n\n ${signature}`); |
Run the signature.js file using the following command:
node signature.js
Output:
Example 2: Sign a file.
const crypto = require( 'crypto' );
const buffer = require( 'buffer' );
const fs = require( 'fs' );
// Create a private key const { privateKey } = crypto.generateKeyPairSync( 'rsa' , {
modulusLength: 2048,
}); console.log( "Reading File...\n" );
// Reading file const text = fs.readFileSync( './doc.txt' );
console.log(`File content: ${text}`); // Convert string to buffer const data = Buffer.from(text); // Sign the data and returned signature in buffer const sign = crypto.sign( "SHA256" , data , privateKey);
// Convert returned buffer to base64 const signature = sign.toString( 'base64' );
// Printing the signature console.log(`Signature:\n\n ${signature}`); |
Run the signature.js file using the following command:
node signature.js
Output:
Example 3: Sign JSON data.
const crypto = require( 'crypto' );
const buffer = require( 'buffer' );
// Create a private key const { privateKey } = crypto.generateKeyPairSync( 'rsa' , {
modulusLength: 2048,
}); // JSON object const person = { name: "Raktim Banerjee" ,
email: "example@gmail.com" ,
address: "4 main street"
} // Convert Stringified json data to buffer const data = Buffer.from( JSON.stringify(person) ); // Sign the data and returned signature in buffer const sign = crypto.sign( "SHA256" , data , privateKey);
// Convert returned buffer to base64 const signature = sign.toString( 'base64' );
// Printing the signature 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