Node.js cipher.final() Method
The cipher.final() method in Node.js is used to signal to the cipher object that the encryption or decryption process is complete. This method must be called after all data has been passed to the cipher object using the cipher.update() method.
The cipher.final() method returns the remaining encrypted or decrypted data as a Buffer object. If the outputEncoding parameter was specified when creating the cipher object, the returned Buffer will be automatically encoded using the specified encoding.
Syntax:
const cipher.final([outputEncoding])
Parameters: This method takes the output encoding as a parameter.
Return Value: This method returns the object of the buffer containing the cipher value.
Example 1: Filename: index.js
Javascript
const crypto = require( 'crypto' );
const algorithm = 'aes-192-cbc' ;
const password = 'Password used to generate key' ;
const key = crypto.scryptSync(password, 'salt' , 24);
const iv = Buffer.alloc(16, 0);
const cipher = crypto.createCipheriv(algorithm, key, iv);
let value = cipher.final( 'hex' );
console.log( "buffer :- " + value);
|
Output:
buffer :- b9be42878310d599e4e49e040d1badb9
Example 2: Filename: index.js
Javascript
const crypto = require( 'crypto' );
const algorithm = 'aes-192-cbc' ;
const password = 'Password used to generate key' ;
crypto.scrypt(password, 'salt' , 24,
{ N: 512 }, (err, key) => {
if (err) throw err;
const iv = Buffer.alloc(16, 0);
const cipher = crypto
.createCipheriv(algorithm, key, iv);
let value = cipher.final( 'hex' );
console.log( "buffer :- " + value);
});
|
Output:
buffer :- 726cccfc7d80ca473d8d4de1a0a42675
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_final_outputencoding
Last Updated :
15 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...