Open In App

Node.js crypto.verify() Function

The crypto.verify()  is a method of the inbuilt module of node.js crypto that is used to verify the signature of data that is hashed using different kinds of hashing functions Like SHA256 algorithm etc.

Syntax:



crypto.verify(algorithm, data, publicKey, signature)

Parameters:

Returned value: This function returns a boolean value. Return True if a signature is verified else false.



Example:

Filename: index.js




// Importing Required Modules
const crypto = require('crypto');
const buffer = require('buffer');
 
// Creating a private key
const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
                                          modulusLength: 2048,
});
// Using Hashing Algorithm
const algorithm = "SHA256";
 
// Converting string to buffer
let data = Buffer.from("I Love GeeksForGeeks");
 
// Sign the data and returned signature in buffer
let signature = crypto.sign(algorithm, data, privateKey);
 
// Verifying signature using crypto.verify() function
let isVerified = crypto.verify(algorithm, data, publicKey, signature);
 
// Printing the result
console.log(`Is signature verified: ${isVerified}`);

Run index.js using the below command:

node index.js

Output:

Reference:https://nodejs.org/api/crypto.html#crypto_crypto_verify_algorithm_data_key_signature

Article Tags :