cipher.final() সাইফার অবজেক্টের মান ধারণকারী একটি বাফার বা স্ট্রিং ফেরত দিতে ব্যবহৃত হয়। এটি একটি অন্তর্নির্মিত পদ্ধতি যা ক্রিপ্টো মডিউলের মধ্যে ক্লাস সাইফার দ্বারা সরবরাহ করা হয়। যদি একটি আউটপুট এনকোডিং নির্দিষ্ট করা হয়, একটি স্ট্রিং ফেরত দেওয়া হয়। একটি আউটপুট এনকোডিং নির্দিষ্ট না হলে একটি বাফার ফেরত দেওয়া হয়। cipher.final পদ্ধতিতে একাধিকবার কল করলে একটি ত্রুটি হবে।
সিনট্যাক্স
cipher.final([outputEncoding])
পরামিতি
উপরোক্ত পরামিতিগুলি নীচে −
হিসাবে বর্ণনা করা হয়েছে-
আউটপুট এনকোডিং - এটি একটি প্যারামিটার হিসাবে আউটপুট এনকোডিং নেয়। এই প্যারামিটারের জন্য ইনপুট টাইপ হল স্ট্রিং। সম্ভাব্য ইনপুট মানগুলি হল হেক্স, বেস64, ইত্যাদি।
উদাহরণ
নাম দিয়ে একটি ফাইল তৈরি করুন – cipherFinal.js এবং নীচের কোড স্নিপেটটি অনুলিপি করুন। ফাইল তৈরি করার পরে, নীচের উদাহরণে দেখানো এই কোডটি চালানোর জন্য নিম্নলিখিত কমান্ডটি ব্যবহার করুন −
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) আউটপুট
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)
উপরের উদাহরণে, আমরা ত্রুটি পাচ্ছি কারণ আমরা ইতিমধ্যে সেই কীটির জন্য সাইফার পেয়েছি। যেহেতু এটি একটি চূড়ান্ত পদ্ধতি তাই এটি ত্রুটি দিচ্ছে যখন আমরা একই কীটির জন্য আবার সাইফার খুঁজে বের করার চেষ্টা করি।