📜  Node.js crypto.createSign() 方法

📅  最后修改于: 2022-05-13 01:56:32.041000             🧑  作者: Mango

Node.js crypto.createSign() 方法

crypto.createSign() 方法用于创建使用所述算法的Sign对象。此外,您可以使用crypto.getHashes() 方法来访问所有可用摘要算法的名称。

句法:

crypto.createSign( algorithm, options )

参数:此方法接受上面提到的两个参数,如下所述:

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

返回值:返回Sign对象。

下面的示例说明了在 Node.js 中使用crypto.createSign() 方法

示例 1:

// Node.js program to demonstrate the 
// crypto.createSign() method
  
// Including crypto module
const crypto = require('crypto');
  
// Defining the algorithm to be used
const algo = 'RSA-SHA256';
  
// Creating Sign object
const sign = crypto.createSign(algo);
  
// Output
console.log(sign);

输出:

Sign {
_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.createSign() method
  
// Including crypto module
const crypto = require('crypto');
  
// Defining the algorithm to be used
const algo = 'SHA256';
  
// Creating Sign object
const sign = crypto.createSign(algo);
  
// Prints true
sign.write('some data to sign');

输出:

true

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