सिफर.फाइनल () का उपयोग बफर या स्ट्रिंग को वापस करने के लिए किया जाता है जिसमें सिफर ऑब्जेक्ट का मान होता है। यह क्रिप्टो मॉड्यूल के भीतर क्लास सिफर द्वारा प्रदान की गई इनबिल्ट पद्धति में से एक है। यदि आउटपुट एन्कोडिंग निर्दिष्ट है, तो एक स्ट्रिंग वापस कर दी जाती है। यदि आउटपुट एन्कोडिंग निर्दिष्ट नहीं है तो एक बफर वापस कर दिया जाता है। cipher. final विधि को एक से अधिक बार कॉल करने से एक त्रुटि होगी।
सिंटैक्स
cipher.final([outputEncoding])
पैरामीटर
उपरोक्त पैरामीटर नीचे वर्णित हैं -
-
आउटपुट एन्कोडिंग - यह आउटपुट एन्कोडिंग को एक पैरामीटर के रूप में लेता है। इस पैरामीटर के लिए इनपुट प्रकार स्ट्रिंग है। संभावित इनपुट मान हेक्स, बेस 64, आदि हैं।
उदाहरण
नाम के साथ एक फाइल बनाएं – cipherFinal.js और नीचे दिए गए कोड स्निपेट को कॉपी करें। फ़ाइल बनाने के बाद, इस कोड को चलाने के लिए निम्न कमांड का उपयोग करें जैसा कि नीचे दिए गए उदाहरण में दिखाया गया है -
node 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) आउटपुट
C:\home\node>> node cipherFinal.js Hex String:- 8d11772fce59f08e7558db5bf17b3112 Base64 String:- jRF3L85Z8I51WNtb8XsxEg==
उदाहरण
आइए एक और उदाहरण देखें।
// 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)
}); आउटपुट
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)
उपरोक्त उदाहरण में, हमें त्रुटि मिल रही है क्योंकि हमें उस कुंजी के लिए पहले ही सिफर मिल गया है। चूंकि यह एक अंतिम विधि है, इसलिए जब हम उसी कुंजी के लिए सिफर को फिर से खोजने का प्रयास करते हैं तो यह त्रुटि दे रहा है।