cipher.final() Method in Node.js

Node.jsJavascriptWeb DevelopmentFront End Technology

The cipher.final() is used to return a buffer or string containing the value of cipher object. It is one of the inbuilt method that is provided by the class Cipher within the crypto module. If an output encoding is specified, a String is returned. If an output encoding is not specified a buffer is returned. Calling the cipher.final method more than once will throw an error.

Syntax

cipher.final([outputEncoding])

Parameters

The above parameters are described as below −

  • outputEncoding – It takes the output encoding as a parameter. The input type for this parameter is string. Possible input values are hex, base64, etc.

Example

Create a file with name – cipherFinal.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below −

node cipherFinal.js

cipherFinal.js

// Example to demonstrate the use of cipher.final() method

// Importing the crypto module
const crypto = require('crypto');

// Initialising the AES algorithm
const algorithm = 'aes-192-cbc';
// Initialising the password used for generating key
const password = '12345678';

// Retrieving key for the cipher object
const key = crypto.scryptSync(password, 'salt', 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);
const cipher2 = crypto.createCipheriv(algorithm, key, iv);

//Getting the string value as outputEncoding is defined
let hexValue = cipher.final('hex');
let base64Value = cipher2.final('base64');

// Printing the result...
console.log("Hex String:- " + hexValue);
console.log("Base64 String:- " + base64Value)

Output

C:\home\node>> node cipherFinal.js
Hex String:- 8d11772fce59f08e7558db5bf17b3112
Base64 String:- jRF3L85Z8I51WNtb8XsxEg==

Example

Let's take a look at one more example.

// Example to demonstrate the use of cipher.final() method

// Importing the crypto module
const crypto = require('crypto');

// Initialising the AES algorithm
const algorithm = 'aes-192-cbc';
// Initialising the password used for generating key
const password = '12345678';

// Retrieving key for the cipher object
const key = crypto.scryptSync(password, 'salt', 24);

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 buffer value since output encoding is null
   let hexValue = cipher.final();
   let base64Value = cipher.final('base64');

   // Printing the result...
   console.log("Buffer:- " + hexValue);
   console.log("Base64 String:- " + base64Value)
});

Output

C:\home\node>> node cipherFinal.js
internal/crypto/cipher.js:164
   const ret = this._handle.final();
                           ^
Error: Unsupported state
   at Cipheriv.final (internal/crypto/cipher.js:164:28)
   at Object. (/home/node/test/cipher.js:22:26)
   at Module._compile (internal/modules/cjs/loader.js:778:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
   at Module.load (internal/modules/cjs/loader.js:653:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
   at Function.Module._load (internal/modules/cjs/loader.js:585:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
   at startup (internal/bootstrap/node.js:283:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

In the above example, we are getting error because we have already got the cipher for that key. Since it is a final method it is giving error when we try to find out the cipher for the same key again.

raja
Published on 20-May-2021 12:06:00
Advertisements