Open In App
Related Articles

Node.js crypto.sign() Function

Improve Article
Improve
Save Article
Save
Like Article
Like

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');
  
// 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.

signature.js




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.

signature.js




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


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
Previous
Next
Similar Reads
Complete Tutorials