Node.js | crypto.createSign() Method

The crypto.createSign() method is used to create a Sign object that uses the stated algorithm. Moreover, you can use crypto.getHashes() method to access the names of all the available digest algorithms.

Syntax:

crypto.createSign( algorithm, options )

Parameters: This method accept two parameters as mentioned above and described below:

  • algorithm: It is a string type value. A Sign instance can be created by applying the name of a signature algorithms, like ‘RSA-SHA256’, in place of a digest algorithms.
  • options: It is an optional parameter that is used to control stream behavior. It returns an object.

Return Value: It returns Sign object.

Below examples illustrate the use of crypto.createSign() method in Node.js:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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 }

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

true

Reference: https://nodejs.org/api/crypto.html#crypto_crypto_createsign_algorithm_options




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.