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.final() Method in Node.js
The cipher.final() method in Node.js returns the remaining encrypted data after all input has been processed through cipher.update(). This method completes the encryption process and can only be called once per cipher instance.
Syntax
cipher.final([outputEncoding])
Parameters
outputEncoding - Optional string parameter specifying the output format. Common values include 'hex', 'base64', 'latin1'. If not provided, returns a Buffer.
Return Value
Returns a Buffer containing the final encrypted data, or a string if outputEncoding is specified.
Example 1: Basic Usage with Different Encodings
// Importing the crypto module
const crypto = require('crypto');
// Algorithm and key setup
const algorithm = 'aes-192-cbc';
const password = '12345678';
const key = crypto.scryptSync(password, 'salt', 24);
const iv = Buffer.alloc(16, 0);
// Create cipher instances for different encodings
const cipher1 = crypto.createCipheriv(algorithm, key, iv);
const cipher2 = crypto.createCipheriv(algorithm, key, iv);
// Encrypt some data first
cipher1.update('Hello World', 'utf8');
cipher2.update('Hello World', 'utf8');
// Get final encrypted data in different formats
const hexResult = cipher1.final('hex');
const base64Result = cipher2.final('base64');
console.log("Hex format:", hexResult);
console.log("Base64 format:", base64Result);
Hex format: 8d11772fce59f08e7558db5bf17b3112 Base64 format: jRF3L85Z8I51WNtb8XsxEg==
Example 2: Returning Buffer (No Encoding)
const crypto = require('crypto');
const algorithm = 'aes-192-cbc';
const password = '12345678';
const key = crypto.scryptSync(password, 'salt', 24);
const iv = Buffer.alloc(16, 0);
const cipher = crypto.createCipheriv(algorithm, key, iv);
// Encrypt data
cipher.update('Secret message', 'utf8');
// Get final result as Buffer (no encoding specified)
const bufferResult = cipher.final();
console.log("Buffer result:", bufferResult);
console.log("Buffer length:", bufferResult.length);
console.log("Buffer as hex:", bufferResult.toString('hex'));
Buffer result: <Buffer 8d 11 77 2f ce 59 f0 8e 75 58 db 5b f1 7b 31 12> Buffer length: 16 Buffer as hex: 8d11772fce59f08e7558db5bf17b3112
Example 3: Error When Called Multiple Times
const crypto = require('crypto');
const algorithm = 'aes-192-cbc';
const password = '12345678';
const key = crypto.scryptSync(password, 'salt', 24);
const iv = Buffer.alloc(16, 0);
const cipher = crypto.createCipheriv(algorithm, key, iv);
cipher.update('Test data', 'utf8');
// First call works fine
const result1 = cipher.final('hex');
console.log("First call result:", result1);
try {
// Second call throws error
const result2 = cipher.final('hex');
console.log("Second call result:", result2);
} catch (error) {
console.log("Error on second call:", error.message);
}
First call result: 8d11772fce59f08e7558db5bf17b3112 Error on second call: Unsupported state
Key Points
cipher.final()can only be called once per cipher instanceMust be called after all data is processed with
cipher.update()Returns remaining encrypted data (padding and final block)
Without encoding parameter, returns a Buffer object
Calling it multiple times throws "Unsupported state" error
Conclusion
The cipher.final() method completes the encryption process and returns the final encrypted data. Always call it once after processing all input data to ensure complete encryption.
