কম্পিউটার

Node.js-এ crypto.scrypt() পদ্ধতি


crypto.scrypt() পদ্ধতি স্ক্রিপ্ট পদ্ধতির জন্য একটি অ্যাসিঙ্ক্রোনাস বাস্তবায়ন প্রদান করে। স্ক্রিপ্টটিকে একটি পাসওয়ার্ড-ভিত্তিক কী ডেরিভেশন ফাংশন হিসাবে সংজ্ঞায়িত করা যেতে পারে যা সিস্টেমকে ব্রুট-ফোর্স অ্যাটাক থেকে রক্ষা করে এবং এটিকে অনভিপ্রেত করে তোলে। কিন্তু স্ক্রিপ্ট ফাংশন গণনাগতভাবে ব্যয়বহুল এবং সেইসাথে মেমরি অনুসারে।

সিনট্যাক্স

crypto.scrypt(password, salt, keylen, [options], [callback])

পরামিতি

উপরের পরামিতিগুলি নীচে −

হিসাবে বর্ণনা করা হয়েছে
  • পাসওয়ার্ড - এন্ট্রি ডিকোড করার জন্য প্রয়োজনীয় স্ক্রিপ্টের জন্য পাসওয়ার্ড ক্ষেত্র। এটি একটি স্ট্রিং, অবজেক্ট, TypedArray, ইত্যাদি হতে পারে।

  • লবণ - এই মান যতটা সম্ভব অনন্য হওয়া উচিত। এটি মূলত ডেটা এনক্রিপ্ট করার জন্য ব্যবহৃত হয়। লবণের সর্বনিম্ন প্রস্তাবিত দৈর্ঘ্য হল 16 বাইট।

  • কিলেন – এই প্যারামিটারটি কীটির দৈর্ঘ্য নির্ধারণ করে এবং একটি সংখ্যা হওয়া উচিত।

  • বিকল্পগুলি

    • খরচ - এটি প্রতিটি মেমরি নেওয়ার জন্য সিপিইউ-এর খরচ। এই মানটি 1 এর থেকে 2 এর বেশি পাওয়ার হওয়া উচিত। ডিফল্ট মান হল 16384।

    • ব্লক সাইজ - এই পরামিতি প্রতিটি ব্লক আকারের জন্য মান সংজ্ঞায়িত করে। ডিফল্ট মান 8.

    • সমান্তরালকরণ - এই পরামিতি সমান্তরালকরণ পরামিতি সংজ্ঞায়িত করে। ডিফল্ট মান হল 1.

    • N - এই প্যারামিটারটি খরচের একটি উপনাম এবং এটির জায়গায় ব্যবহার করা যেতে পারে। একটি সময়ে শুধুমাত্র একটি সংজ্ঞায়িত করা যেতে পারে৷

    • আর – এই প্যারামিটারটি ব্লক সাইজের একটি উপনাম এবং একইভাবে শুধুমাত্র একটিকে একবারে সংজ্ঞায়িত করা যেতে পারে৷

    • p - সমান্তরালকরণের জন্য উপনাম। শুধুমাত্র একটি সংজ্ঞায়িত করা যেতে পারে।

    • ম্যাক্সমেম - এটি মেমরি উচ্চ সীমাবদ্ধ মান. 128*N*r> maxmem হলে একটি ত্রুটি নিক্ষেপ করা হয়। ডিফল্ট মান হল 32*1024*1024।

  • কলব্যাক – আপনি যদি এটি পরিচালনা করতে চান তবে একটি ত্রুটি নিক্ষেপ করা হলে এই ফাংশনটিকে বলা হয়৷

উদাহরণ

নাম দিয়ে একটি ফাইল তৈরি করুন – scrypt.js এবং নিচের কোড স্নিপেটটি কপি করুন। ফাইল তৈরি করার পরে, নীচের উদাহরণে দেখানো এই কোডটি চালানোর জন্য নিম্নলিখিত কমান্ডটি ব্যবহার করুন −

node scrypt.js

scrypt.js

// Node.js program to demonstrate the flow of crypto.scrypt() method

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

// Calling the scrypt() method with the below parameters
crypto.scrypt('tutorialspoint', 'asdfghjkl', 32, (err, derivedKey) => {

if (err) throw err;

// Prints the derived key as a buffer value
console.log("The derived key(1) is:", derivedKey);
});

// Calling the scrypt() method with the cost option
crypto.scrypt('GeeksforGeeks', 'tfytdx', 128,
   { N: 512 }, (err, derivedKey) => {

if (err) throw err;

// Prints the derived key as a buffer value
console.log("The derived key(2) is:", derivedKey);
console.log();
});

আউটপুট

C:\home\node>> node scrypt.js
The derived key(2) is: <Buffer b3 f8 72 5f 58 df 98 d9 c0 8a ba 0c 2c 50 85 b1
76 de 39 35 40 27 7d 57 f1 6a a1 07 54 dc c9 63 65 32 f2 db 29 95 dc ee 0b 9f
e3 d5 0a 9e 3a d0 f6 b4 ... >

The derived key(1) is: <Buffer ae 50 38 61 17 f7 11 51 e4 50 63 3c 2a 9c ec f0
46 42 a6 ca 04 78 67 05 c8 8c 0c 69 00 c3 03 7f>

উদাহরণ

আসুন আরও একটি উদাহরণ দেখি।

// Node.js program to demonstrate the flow of crypto.scrypt() method

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

// Initializing the value of salt as a typedArray
const salt = new Uint32Array(7);

// Using the scrypt() method with below parameters
crypto.scrypt('WelcomeTutorialspoint', salt, 16, (err, derivedKey) => {
   if (err) throw err;
   // Printing the derived key in encoded string format
   console.log("The derived key(1) is:",
      derivedKey.toString("ascii"));
});

// Initialising the value of salt as a DataView
const newSalt = new DataView(new ArrayBuffer(5));

// Using the script() method with cost parameter
crypto.scrypt('HelloTutorialspoint', newSalt, 16, { N: 32 }, (err, derivedKey) => {
   if (err) throw err;
   // Printing the derived key in encoded string format
   console.log("The derived key(2) is:",
      derivedKey.toString("base64"));
});

আউটপুট

C:\home\node>> node scrypt.js
The derived key(2) is: PBYDRlgayLVGjC8z3YUcSQ==
The derived key(1) is: <Uu0allCb,d?,Z5_

  1. Node.js-এ crypto.publicDecrypt() পদ্ধতি

  2. Node.js-এ crypto.privateEncrypt() পদ্ধতি

  3. Node.js-এ crypto.privateDecrypt() পদ্ধতি

  4. Node.js-এ crypto.getHashes() পদ্ধতি