Node.js cipher.update() Method - GeeksforGeeks

16 Sep, 2021
The cipher.update() method is an inbuilt application programming interface of class Cipher within crypto module which is used to update the cipher with data according to the given encoding format.
Syntax:
const cipher.update(data[, inputEncoding][, outputEncoding])
Parameters: This method takes the following parameter:
data: It is used to update the cipher by new content.
inputEncoding: Input encoding format.
outputEncoding: Output encoding format.
Return Value: This method returns the object of buffer containing the cipher value.
Example 1: Filename: index.js
Javascript
// Node.js program to demonstrate the// cipher.update() method // Importing crypto moduleconst crypto = require('crypto'); // Creating and initializing algorithm and passwordconst algorithm = 'aes-192-cbc';const password = 'Password used to generate key'; // Getting key for the cipher objectconst key = crypto.scryptSync(password, 'salt', 24); // Creating and initializing the static ivconst iv = Buffer.alloc(16, 0); // Creating and initializing the cipher objectconst cipher = crypto.createCipheriv(algorithm, key, iv); // Updating the cipher with the data// by using update() methodlet encrypted = cipher.update(    'some clear text data', 'utf8', 'hex'); // Getting the buffer data of cipherencrypted += cipher.final('hex'); // Display the resultconsole.log(encrypted);
Output:
e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa
Example 2: Filename: index.js
Javascript
// Node.js program to demonstrate the// cipher.update() method // Importing crypto moduleconst crypto = require('crypto'); // Creating and initializing algorithm and passwordconst algorithm = 'aes-192-cbc';const password = 'Password used to generate key'; // Getting key for cipher objectcrypto.scrypt(password, 'salt', 24,    { N: 512 }, (err, key) => {        if (err) throw err;         // Creating and initializing the static iv        const iv = Buffer.alloc(16, 0);         // Creating and initializing the cipher object        const cipher = crypto            .createCipheriv(algorithm, key, iv);         // Updating the cipher with the data        // by using update() method        let encrypted = cipher.update(            'some clear text data', 'utf8', 'hex');         // Getting the buffer data of cipher         encrypted += cipher.final('hex');         // Display the result        console.log(encrypted);    });
Output:
5850288b1848440f0c410400403f7b456293229b5231c17d2b83b602f252714b
Run the index.js file using the following command:
node index.js
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/crypto.html#crypto_cipher_update_data_inputencoding_outputencoding
gulshankumarar231
sweetyty
Node.js-crypto-module
Node.js-Methods
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to update Node.js and NPM to next version ?
Node.js fs.readFileSync() Method
Node.js fs.writeFile() Method
How to update NPM ?
Difference between promise and async await in Node.js
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript