Open In App

Node.js sign.update(data[, inputEncoding]) Method

In this article, we will discuss about the sign.update() method in the sign class of the crypto module in NodeJS. This method updates the sign content with the given data and input encoding. If the provided data object is a Buffer, TypeArray, or DataView, then any given inputEncoding parameter is ignored.

Syntax:



sign.update(data, inputEncoding)

Parameters: This method has two parameters as mentioned above.

Returns: This method does not return any values.



Example 1: In the below example, we are printing the sign method using console log and changing the sign content by giving the data parameter.




const crypto = require('crypto');
const { privateKey, publicKey } = 
    crypto.generateKeyPairSync('rsa', {
    modulusLength: 2048,
});
  
const sign = crypto.createSign('SHA256');
  
console.log(sign.update('GeeksforGeeks'));
  
sign.end();

Output:

Sign {
 _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,
   afterWriteTickInfo: null,
   buffered: [],
   bufferedIndex: 0,
   allBuffers: true,
   allNoop: true,
   pendingcb: 0,
   constructed: true,
   prefinished: false,
   errorEmitted: false,
   emitClose: true,
   autoDestroy: true,
   errored: null,
   closed: false,
   closeEmitted: false,
   [Symbol(kOnFinished)]: []
 },
 _events: [Object: null prototype] {},
 _eventsCount: 0,
 _maxListeners: undefined,
 [Symbol(kHandle)]: Sign {},
 [Symbol(kCapture)]: false
}

Example 2: In the below example, we are verifying the content of the sign method using the verify.verify() method.




const crypto = require('crypto');
const { privateKey, publicKey } =
    crypto.generateKeyPairSync('rsa', {
        modulusLength: 2048,
    });
  
const sign = crypto.createSign('SHA256');
sign.update('GeeksforGeeks');
sign.end();
  
const signature = sign.sign(privateKey);
  
const verify = crypto.createVerify('SHA256');
verify.update('GeeksforGeeks')
verify.end();
console.log(verify.verify(publicKey, signature));

Output:

true

Reference: https://nodejs.org/api/crypto.html#signupdatedata-inputencoding


Article Tags :