Open In App

Node.js crypto.createVerify() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The crypto.createVerify() method is used to create a Verify object that uses the stated algorithm. Moreover, you can use crypto.getHashes() to access the names of all the available signing algorithms.

Syntax:

crypto.createVerify( algorithm, options )

Parameters: This method accept two parameters as mentioned above and described below:

  • algorithm: It is a string type value. A Sign instance can be created by applying the name of a signature algorithms, like ‘RSA-SHA256’, in place of a digest algorithms.
  • options: It is an optional parameter that is used to control stream behavior. It returns an object.

Return Value: It returns Verify object.

Below examples illustrate the use of crypto.createVerify() method in Node.js:

Example 1:




// Node.js program to demonstrate the 
// crypto.createVerify() method
  
// Including crypto module
const crypto = require('crypto');
  
// Creating verify object with its algo
const verify = crypto.createVerify('SHA256');
  
// Returns the 'Verify' object
console.log(verify);


Output:

Verify {
  _handle: {},
  _writableState:
   WritableState {
     objectMode: false,
     highWaterMark: 16384,
     finalCalled: false,
     needDrain: false,
     ending: false,
     ended: false,
     finished: false,
     destroyed: false,
     decodeStrings: true,
     defaultEncoding: 'utf8',
     length: 0,
     writing: false,
     corked: 0,
     sync: true,
     bufferProcessing: false,
     onwrite: [Function: bound onwrite],
     writecb: null,
     writelen: 0,
     bufferedRequest: null,
     lastBufferedRequest: null,
     pendingcb: 0,
     prefinished: false,
     errorEmitted: false,
     emitClose: true,
     autoDestroy: false,
     bufferedRequestCount: 0,
     corkedRequestsFree:
      { next: null,
        entry: null,
        finish: [Function: bound onCorkedFinish] } },
  writable: true,
  domain: null,
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined }

Example 2:




// Node.js program to demonstrate the 
// crypto.createVerify() method
  
// Including crypto module
const crypto = require('crypto');
  
// Creating verify object with its algo
const verify = crypto.createVerify('SHA256');
  
// Writing data to be signed and verified
verify.write('some text to sign');
  
// Calling end method
verify.end();
  
    // Beginning public key
    const l1 = "-----BEGIN PUBLIC KEY-----\n"
      
    // Encrypted data
    const l2 =
  "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEXIvPbzLjaPLd8jgiv1TL/X8PXpJNgDkGRj9U9Lcx1yKURpQFVavcMkfWyO8r7JlZNMax0JKfLZUM1IePRjHlFw=="
  
    // Ending public key
    const l3 = "\n-----END PUBLIC KEY-----"
  
    // Constructing public key
    const publicKey = l1 + l2 + l3
  
    // Signature to be verified
    const signature = "MEYCIQCPfWhpzxMqu3gZWflBm5V0aetgb2/S+SGyGcElaOjgdgIhALaD4lbxVwa8HUUBFOLz+CGvIioDkf9oihSnXHCqh8yV";
  
    // Prints true if verified else false
    console.log(verify.verify(publicKey, signature));


Output:

false

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



Last Updated : 11 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads