cipher.update() Method in Node.js


The cipher.update() is used to update the cipher with the receivd data according to the given encoding format. It is one of the inbuilt method that is provided by the class Cipher within the crypto module. If an input encoding is specified, the data argument is a string, else the data argument is a buffer

Syntax

cipher.update(data, [inputEncoding], [outputEncoding])

Parameters

The above parameters are described as below −

  • data – It takes the data as an input that is passed to update the cipher content.

  • inputEncoding – It takes the input encoding as a parameter. Possible input values are hex, base64, etc.

  • outputEncoding – It takes the output encoding as a parameter. The input type for this parameter is string. Possible input values are hex, base64, etc.

Example

Create a file with name – cipherUpdate.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 cipherUpdate.js

cipherUpdate.js

// Example to demonstrate the use of cipher.final() method

// Importing the crypto module
const crypto = require('crypto');

// Initialising the AES algorithm
const algorithm = 'aes-192-cbc';
// Initialising the password used for generating key
const password = '12345678123456789';

// Retrieving key for the cipher object
const key = crypto.scryptSync(password, 'old data', 24);

// Initializing the static iv
const iv = Buffer.alloc(16, 0);

// Initializing the cipher object to get cipher
const cipher = crypto.createCipheriv(algorithm, key, iv);

//Getting the updated string value with new data
let updatedValue = cipher.update('Welcome to tutorials point', 'utf8', 'hex');

//Adding the old value and updated value
updatedValue += cipher.final('hex');

// Printing the result...
console.log("Updated String:- " + updatedValue);

Output

C:\home
ode>> node cipherUpdate.js Updated String:- a05e87569f3f04234812ae997da5684944c32b8776fae676b4abe9074b31cd2a

Example

Let's take a look at one more example.

// Example to demonstrate the use of cipher.final() method

// Importing the crypto module
const crypto = require('crypto');

// Initialising the AES algorithm
const algorithm = 'aes-192-cbc';
// Initialising the password used for generating key
const password = '12345678123456789';

// Retrieving key for the cipher object
crypto.scrypt(password, 'salt', 24,
   { N: 512 }, (err, key) => {
      if (err) throw err;

   // Initializing the static iv
   const iv = Buffer.alloc(16, 0);

   // Initializing the cipher object to get cipher
   const cipher = crypto.createCipheriv(algorithm, key, iv);

   //Getting the updated string value with new data
   let updatedValue = cipher.update('Some new text data', 'utf8', 'hex');
   //Adding the old value and updated value
   updatedValue += cipher.final('hex');

   // Printing the result...
   console.log("Updated String:- " + updatedValue);
});

Output

C:\home
ode>> node cipherUpdate.js Updated String:- 91d6d37e70fbae537715f0a921d15152194435b96ce3973d92fbbc4a82071074

Updated on: 20-May-2021

435 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements