crypto.createVerify() Method in Node.js


The crypto.createVerify() will create and return a verify object that uses the passed algorithm in the parameter. One can use, crypto.getHashes() to get the names of all the available signing algorithms. You can create a Verify instance by using the name of the signature algorithms such as 'RHA-SHA256' only in some of the cases, instead of a digest algorithm.

Syntax

crypto.createVerify(algorithm, [options])

Parameters

The above parameters are described as below −

  • algorithm – It takes the input for the algorithm name to be used while creating the verify object/instance.

  • options – This is an optional parameter that can be used for controlling the stream behaviour.

Example

Create a file with name – createVerify.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below −

node createVerify.js

createVerify.js

 Live Demo

// Node.js program to demonstrate the use of createVerify() method

// Importing the crypto module
const crypto = require('crypto');

// Creating verify object with the input algorithm
const verify = crypto.createVerify('SHA256');

// Returning the verify object
console.log(verify);

Output

C:\home
ode>> node createVerify.js 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,    _events: [Object: null prototype] {},    _eventsCount: 0,    _maxListeners: undefined }

Example

Let's take a look at one more example.

// Node.js program to demonstrate the use of createVerify() method

// Importing the crypto module
const crypto = require('crypto');

// Creating the verify object from SHA256 algo
const verify = crypto.createVerify('SHA256');

// Writing the below data to be signed and verified
verify.write('TutorialPoint');

// Ending the method
verify.end();

   // Beginning public key execution
   const l1 = "-----BEGIN PUBLIC KEY-----
"    // Encrypted data    const l2 = "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEXIvPbzLjaPLd8jgiv1TL/X8PXpJNgDkGRj9U9Lcx1 yKURpQFVavcMkfWyO8r7JlZNMax0JKfLZUM1IePRjHlFw=="    // Finishing public key execution    const l3 = "
-----END PUBLIC KEY-----"    // concatenating all public keys    const publicKey = l1 + l2 + l3    // Signature that will be verified    const signature = "MEYCIQCPfWhpzxMqu3gZWflBm5V0aetgb2/S+SGyGcElaOjgdgIhALaD4lbxVwa8HUUBFOLz+CGvI ioDkf9oihSnXHCqh8yV";    // Prints true if signature is verified else false    console.log(verify.verify(publicKey, signature));

Output

C:\home
ode>> node createVerify.js false

Updated on: 20-May-2021

360 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements