Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
cipher.update() Method in Node.js
The cipher.update() method is used to update the cipher with data according to the specified encoding format. It is one of the built-in methods provided by the Cipher class within the crypto module. If an input encoding is specified, the data argument is a string; otherwise, the data argument is a buffer.
Syntax
cipher.update(data, [inputEncoding], [outputEncoding])
Parameters
The parameters are described as follows:
-
data - The data to be encrypted. Can be a string or buffer.
-
inputEncoding - The encoding of the input data. Common values include 'utf8', 'hex', 'base64'.
-
outputEncoding - The encoding for the output. Common values include 'hex', 'base64'. If not specified, returns a buffer.
Return Value
Returns the encrypted content. If outputEncoding is specified, returns a string; otherwise returns a buffer.
Example 1: Basic Encryption
// Importing the crypto module
const crypto = require('crypto');
// Initializing the AES algorithm
const algorithm = 'aes-192-cbc';
// Initializing 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 final encrypted chunk
updatedValue += cipher.final('hex');
// Printing the result
console.log("Encrypted String: " + updatedValue);
Encrypted String: a05e87569f3f04234812ae997da5684944c32b8776fae676b4abe9074b31cd2a
Example 2: Using Async Key Generation
// Importing the crypto module
const crypto = require('crypto');
// Initializing the AES algorithm
const algorithm = 'aes-192-cbc';
// Initializing the password used for generating key
const password = '12345678123456789';
// Retrieving key for the cipher object asynchronously
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 final encrypted chunk
updatedValue += cipher.final('hex');
// Printing the result
console.log("Encrypted String: " + updatedValue);
});
Encrypted String: 91d6d37e70fbae537715f0a921d15152194435b96ce3973d92fbbc4a82071074
Key Points
-
cipher.update()can be called multiple times to encrypt data in chunks. -
Always call
cipher.final()after allupdate()calls to get the complete encrypted result. -
The method is useful for streaming encryption of large datasets.
-
Input and output encodings are optional but commonly used for string operations.
Conclusion
The cipher.update() method provides flexible data encryption with encoding options. Use it with cipher.final() to complete the encryption process for secure data transformation.
