Computer >> Computer tutorials >  >> Programming >> Javascript

crypto.createSign() Method in Node.js


The crypto.createSign() will create and return a sign object tha uses the passed algorithm in the parameter. One can use, crypto.getHashes() to get the names of all the available digest algorithms. You can create a Sign instance by using the name of the signature algorithms such as 'RHA-SHA256' only in some of the cases, instead of a digest algorithm.

Syntax

crypto.createSign(algorithm, [options])

Parameters

The above parameters are described as below −

  • algorithm – It takes the input for the algorithm name to be used while creating the sign object/instance.

  • options – This is an optional parameter that can be used for controlling the stream behaviour.

Example

Create a file with name – createSign.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below −

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

Output

C:\home\node>> 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 }

Example

Let's take a look at one more example.

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

Output

C:\home\node>> node createSign.js
true