Skip to content
Related Articles

Related Articles

Node.js cipher.final() Method
  • Last Updated : 09 Aug, 2020

The cipher.final() method is an inbuilt application programming interface of class Cipher within crypto module which is used to return the buffer containing the value of cipher object.

Syntax:

const cipher.final([outputEncoding])

Parameters: This method takes the output encoding as a parameter.

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.final() method
  
// Importing crypto module
const crypto = require('crypto');
  
// Creating and initializng algorith and password
const algorithm = 'aes-192-cbc';
const password = 'Password used to generate key';
  
// Getting key for the cipher object
const key = crypto.scryptSync(password, 'salt', 24);
  
// Creating and initializg the static iv
const iv = Buffer.alloc(16, 0);
  
// Creating and initializng the cipher object
const cipher = crypto.createCipheriv(algorithm, key, iv);
  
// Getting buffer value 
// by using final() method
let value = cipher.final('hex');
  
// Display the result
console.log("buffer :- " + value);

Output:

buffer :- b9be42878310d599e4e49e040d1badb9

Example 2: Filename: index.js

Javascript




// Node.js program to demonstrate the
// cipher.final() method
  
// Importing crypto module
const crypto = require('crypto');
  
// Creating and initializng algorith and password
const algorithm = 'aes-192-cbc';
const password = 'Password used to generate key';
  
// Getting key for cipher object
crypto.scrypt(password, 'salt', 24,
    { N: 512 }, (err, key) => {
  
        if (err) throw err;
  
        // Creating and initializg the static iv
        const iv = Buffer.alloc(16, 0);
  
        // Creating and initializng the cipher object
        const cipher = crypto
            .createCipheriv(algorithm, key, iv);
  
        // Getting buffer value 
        // by using final() method
        let value = cipher.final('hex');
  
        // Display the result
        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

My Personal Notes arrow_drop_up
Recommended Articles
Page :