Open In App

Node.js verify.verify(object, signature[, signatureEncoding]) Method

Last Updated : 11 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss about the verify.verify() method in the Verify Class of the crypto module in NodeJS. The verify class utility is used for verifying the signature. This method verifies the signature by providing the given data object and signature.

The verify object can not be used again after verify.verify() has been called. Multiple times to call verify.verify() method will result in an error being thrown.

Syntax:

verify.verify(object, signature, signatureEncoding)

Parameter: This method accepts three parameters as mentioned above:

  • object: This will be the object that has to be verified. It can be of the type Object, string, ArrayBuffer, Buffer, TypedArray, DataView, KeyObject or CryptoKey.
  • signature: This parameter defines which type of signature using like string, ArrayBuffer, Buffer, TypedArray or DataView.
  • signatureEncoding: This parameter specifies the encoding of the data.

Returns: This method returns a Boolean value. If the signature matches, then it will return true, otherwise false.

Example 1: In the below example, we will verify the two signatures. In this case, as both signatures are the same so it will return true.

Javascript




const crypto = require('crypto');
  
const { privateKey, publicKey } =
    crypto.generateKeyPairSync('rsa', {
        modulusLength: 2048,
    });
  
const sign = crypto.createSign('SHA256');
sign.update('GeeksforGeeks');
sign.end();
  
const signature = sign.sign(privateKey);
  
const verify = crypto.createVerify('SHA256');
verify.update('GeeksforGeeks')
verify.end();
  
console.log(verify.verify(publicKey, signature));


Output:

true

Example 2: In the below example, We are verifying the two different signatures.

Javascript




const crypto = require('crypto');
  
const { privateKey, publicKey } =
    crypto.generateKeyPairSync('rsa', {
        modulusLength: 2048,
    });
  
const sign = crypto.createSign('SHA256');
sign.update('GeeksforGeeks');
sign.end();
  
const signature = sign.sign(privateKey);
  
const verify = crypto.createVerify('SHA256');
verify.update('Not Same')
verify.end();
  
console.log(verify.verify(publicKey, signature));


Output:

false

Reference: https://nodejs.org/api/crypto.html#verifyverifyobject-signature-signatureencoding



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads