ভূমিকা
cipher.final() সাইফার অবজেক্টের ভ্যালু ধারণকারী একটি বাফার বা স্ট্রিং রিটার্ন করে। এটি crypto মডিউলের Cipher ক্লাসের একটি বিল্ট-ইন মেথড। আউটপুট এনকোডিং স্পেসিফাই থাকলে স্ট্রিং, নাহলে বাফার রিটার্ন করে। cipher.final মেথড একবারের বেশি কল করলে এরর থ্রো করে।
সিনট্যাক্স
cipher.final([outputEncoding])
প্যারামিটার
outputEncoding — আউটপুট এনকোডিং। ইনপুট টাইপ: স্ট্রিং। সম্ভব মান: hex, base64, ইত্যাদি।
উদাহরণ ১
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) ...
উপরের উদাহরণে একই কি ব্যবহার করে دوبার cipher.final() কল করা হয়েছে, তাই এরর।