当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js crypto.createVerify()用法及代码示例


crypto.createVerify()方法用于创建使用所述算法的验证对象。此外,您可以使用crypto.getHashes()来访问所有可用签名算法的名称。

用法:

crypto.createVerify( algorithm, options )

参数:该方法接受上述和以下所述的两个参数:



  • algorithm:它是一个字符串类型值。可以通过应用签名算法(例如“ RSA-SHA256”)的名称来代替摘要算法来创建Sign实例。
  • options:它是一个可选参数,用于控制流的行为。它返回一个对象。

返回值:它返回验证对象。

以下示例说明了Node.js中crypto.createVerify()方法的使用:

范例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);

输出:

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 }

范例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));

输出:

false

参考: https://nodejs.org/api/crypto.html#crypto_crypto_createverify_algorithm_options




相关用法


注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 Node.js | crypto.createVerify() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。