উভয় crypto.randomFill() পদ্ধতি এবং crypto.randomBytes() পদ্ধতি প্রায় একই। উভয়ের মধ্যে পার্থক্য হল – randomFill() পদ্ধতিতে প্রথম আর্গুমেন্ট হল একটি বাফার যা পূরণ করা হবে। এটিতে একটি কলব্যাক পদ্ধতিও রয়েছে যা কলব্যাক কনফিগার করা থাকলে শুধুমাত্র একটি ত্রুটির সম্মুখীন হলে বলা হয়৷
সিনট্যাক্স
crypto.randomFill(buffer, [offset], [size], [callback])
পরামিতি
উপরোক্ত পরামিতিগুলি নীচে −
হিসাবে বর্ণনা করা হয়েছে-
বাফার - এই ক্ষেত্রটিতে ডেটা সামগ্রী রয়েছে। সম্ভাব্য বাফার প্রকারগুলি হল:স্ট্রিং, টাইপডঅ্যারে, বাফার, অ্যারেবাফার, ডেটাভিউ। বাফারের আকার 2**31-1 এর বেশি হতে পারে না।
-
অফসেট - অফসেটের মান যেখান থেকে র্যান্ডমফিল শুরু হবে। ডিফল্ট মান 0।
-
আকার - অফসেটের পরে বাফারের আকার। অর্থাৎ (buffer.length-offset)। এই মান 2**31-1 এর বেশি হতে পারে না।
-
কলব্যাক – যে ফাংশনটিকে কল করা হবে যখন একটি ত্রুটি নিক্ষেপ করা হয়।
উদাহরণ
নাম দিয়ে একটি ফাইল তৈরি করুন – randomFill.js এবং নীচের কোড স্নিপেটটি অনুলিপি করুন। ফাইল তৈরি করার পরে, নীচের উদাহরণে দেখানো এই কোডটি চালানোর জন্য নিম্নলিখিত কমান্ডটি ব্যবহার করুন −
node randomFill.js
randomFill.js
crypto.randomFill() পদ্ধতির প্রবাহ প্রদর্শনের জন্য// Node.js program to demonstrate the flow of crypto.randomFill() method
// Importing the crypto module
const crypto = require('crypto');
const fs = require("fs");
// Initialising the buffer bytes value
const buf = Buffer.alloc(6);
// Calling the randomFill method with buffer and a callback
crypto.randomFill(buf, (err, buf) => {
if (err) throw err;
// Printing the buffer data value after filling
console.log(buf.toString('ascii'));
});
//Calling the randomFill with all the parameters defined above
crypto.randomFill(buf, 3, 2, (err, buf) => {
if (err) throw err;
// Printing the new random data in buffer
console.log(buf.toString('base64'));
});
// We can see that the output will be same for below function
crypto.randomFill(buf, 3, 3, (err, buf) => {
if (err) throw err;
console.log(buf.toString('base64'));
}); আউটপুট
C:\home\node>> node randomFill.js f!]"+– ZqHdoit8 ZqHdoit8
উদাহরণ
আসুন আরও একটি উদাহরণ দেখি।
crypto.randomFill() পদ্ধতির প্রবাহ প্রদর্শনের জন্য// Node.js program to demonstrate the flow of crypto.randomFill() method
// Importing the crypto module
const crypto = require('crypto');
const fs = require("fs");
// Initialising the buffer bytes value using data view
const data = new DataView(new ArrayBuffer(16));
// Calling the randomFill method with buffer and a callback
crypto.randomFill(data, (err, buf) => {
if (err) throw err;
// Printing the randomFill data with encoding
console.log(Buffer.from(buf.buffer,
buf.byteOffset, buf.byteLength).toString('ascii'));
}); আউটপুট
C:\home\node>> node randomFill.js >h(Be#D8h0