Node.js crypto.verify() Function
Last Updated :
04 Apr, 2023
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:
- algorithm: It is a string-type value. A signature can be verified by applying the name of signature algorithms, like ‘SHA256’. The algorithm must be the same in which the signature was created.
- data: The data argument must be an instance of the buffer, Typed Array, or Data View.
- key: It should be the public key of the key object. If you do have not any public keys then you can create private and public keys using crypto.generateKeyPairSync() method.
- signature: The signature argument must be an instance of Buffer, Typed Array, or Data View.
Returned value: This function returns a boolean value. Return True if a signature is verified else false.
Example:
Filename: index.js
Javascript
const crypto = require( 'crypto' );
const buffer = require( 'buffer' );
const { privateKey, publicKey } = crypto.generateKeyPairSync( 'rsa' , {
modulusLength: 2048,
});
const algorithm = "SHA256" ;
let data = Buffer.from( "I Love GeeksForGeeks" );
let signature = crypto.sign(algorithm, data, privateKey);
let isVerified = crypto.verify(algorithm, data, publicKey, signature);
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...