Node.js 中的 crypto.createSign() 方法
crypto.createSign() 将创建和返回一个 sign 对象,该对象使用参数中传递的算法。你可以使用 crypto.getHashes() 获取所有可用的摘要算法的名称。你可以使用签名算法的名称(例如“RHA-SHA256”)创建 Sign 实例,仅在某些情况下,而不是摘要算法。
语法
crypto.createSign(algorithm, [options])
参数
上述参数的描述如下 −
algorithm – 它输入创建 sign 对象/实例时要使用的算法名称。
options – 这是一个可选参数,可用于控制流的行为。
示例
创建一个名为 createSign.js 的文件并复制以下代码段。创建文件后,使用以下命令运行此代码,如以下示例所示 −
node createSign.js
createSign.js
// Node.js program to demonstrate the use of createSign() method // Importing the crypto module const crypto = require('crypto'); // Creating sign object with the input algorithm const sign = crypto.createSign('SHA256'); // Returning the sign object console.log(sign);
输出
C:\home
ode>> node createSign.js 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, _events: [Object: null prototype] {}, _eventsCount: 0, _maxListeners: undefined }
示例
我们再看一个例子。
// Node.js program to demonstrate the use of createSign() method // Importing the crypto module const crypto = require('crypto'); // Creating sign object with the input algorithm const sign = crypto.createSign('SHA256'); // Returning the sign object console.log(sign.write('Welcome to Tutorials Point'));
输出
C:\home
ode>> node createSign.js true
广告