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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP