Node.js 中的 crypto.createVerify() 方法


crypto.createVerify() 將建立一個並返回一個驗證物件,該物件使用引數中傳遞的演算法。可以使用 crypto.getHashes() 獲取所有可用簽名演算法的名稱。可以使用簽名演算法的名稱(例如“RHA-SHA256”)建立一個 Verify 例項,僅在某些情況下,而不是摘要演算法。

語法

crypto.createVerify(algorithm, [options])

引數

以下是上述引數的說明 −

  • algorithm – 它獲取用於在建立驗證物件/例項時使用的演算法名稱。

  • options – 這是一個可選引數,可用於控制流行為。

示例

建立一個名為 createVerify.js 的檔案,並複製以下程式碼段。建立檔案後,使用以下命令執行此程式碼,如下面的示例所示 −

node createVerify.js

createVerify.js

 即時演示

// 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);

輸出

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 }

示例

我們再來看一個示例。

// 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));

輸出

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

更新於: 2021 年 5 月 20 日

661 次檢視

開啟你的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.