কম্পিউটার

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


crypto.generateKeyPair() নির্দিষ্ট ধরনের একটি নতুন অপ্রতিসম কী জোড়া তৈরি করতে ব্যবহার করা যেতে পারে। কী জোড়া তৈরি করার জন্য সমর্থিত প্রকারগুলি হল:RSA, DSA, EC, Ed25519, Ed448, X25519, X448 এবং DH৷ ফাংশনটি এমন আচরণ করে যেন keyObject.export এর ফলাফলে কল করা হয়েছে যখন একটি publicKeyEncoding বা privateKeyEncoding নির্দিষ্ট করা হয়, অন্যথায় keyObject-এর সংশ্লিষ্ট অংশ ফেরত দেওয়া হয়।

সিনট্যাক্স

crypto.generateKeyPair(type, options, callback)

পরামিতি

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

হিসাবে বর্ণনা করা হয়েছে
  • টাইপ - এটি স্ট্রিং টাইপ ধারণ করে যার জন্য কী তৈরি করা প্রয়োজন। সমর্থিত প্রকারগুলি হল - RSA, DSA, EC, Ed25519, Ed448, X25519, X448 এবং DH৷

  • বিকল্পগুলি ৷ - এটি নিম্নলিখিত পরামিতিগুলি ধরে রাখতে পারে -

    • মডুলাস দৈর্ঘ্য - এটি টাইপের (RSA, DSA) জন্য বিটগুলিতে কী আকার ধারণ করে।

    • পাবলিক এক্সপোনেন্ট - এটি RSA অ্যালগরিদমের জন্য সর্বজনীন সূচক মান রাখে।

      ডিফল্ট মান হল – 0x10001

    • ভাজক দৈর্ঘ্য - এটি বিটগুলিতে q এর আকার ধারণ করে।

    • নামযুক্ত কার্ভ – এটি ব্যবহার করার জন্য বক্ররেখার নাম ধরে রাখবে।

    • প্রধান - এটি DH

      -এর মত প্রকারের জন্য প্রাইম প্যারামিটার ধরে রাখবে
    • প্রধান দৈর্ঘ্য – এটি প্রাইম লেন্থকে বিটে ধরে রাখবে।

    • জেনারেটর – এই প্যারামিটারটি কাস্টম জেনারেটর মান ধারণ করে, ডিফল্ট:2।

    • গোষ্ঠীর নাম – এটি ডিএইচ অ্যালগরিদমের ডিফে-হেলম্যান গ্রুপের নাম।

    • publicKeyEncoding – এটি সর্বজনীন কী এনকোডিংয়ের জন্য স্ট্রিং মান ধরে রাখবে।

    • privateKeyEncoding - এটি ব্যক্তিগত কী এনকোডিংয়ের জন্য স্ট্রিং মান ধরে রাখবে৷

  • কলব্যাক - কলব্যাকের নিম্নলিখিত পরামিতি রয়েছে −

    • ভুল - এটি ত্রুটির ধরন ধরে রাখবে।

    • পাবলিক কী - এটি সর্বজনীন কী-এর মান ধরে রাখবে।

    • ব্যক্তিগত কী - এটি ব্যক্তিগত কী-এর মান ধরে রাখবে।

উদাহরণ

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

node generateKeyPair.js

generateKeyPair.js

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

// Importing generateKeyPair from crypto module
const { generateKeyPair } = require('crypto');

// Calling generateKeyPair() method with the below parameters
generateKeyPair('rsa', {
   modulusLength: 530, // options
   publicExponent: 0x10101,
   publicKeyEncoding: {
      type: 'pkcs1',
      format: 'der'
   },
   privateKeyEncoding: {
      type: 'pkcs8',
      format: 'der',
      cipher: 'aes-192-cbc',
      passphrase: 'Welcome to TutorialsPoint!'
   }
}, (err, publicKey, privateKey) => { // Callback function
   if(!err)
   {
      // This will print the asymmetric key pair
      console.log("Public Key is: ", publicKey);
      console.log();
      console.log("Private Key is: ", privateKey);
   }
   else
   {
      // Prints error if any
      console.log("Errr is: ", err);
   }
});

আউটপুট

C:\home\node>> node generateKeyPair.js
Public Key is: <Buffer 30 4a 02 43 03 43 66 5a e1 25 0d d8 c4 fe e3 a0 77 ea
e4 e7 20 78 8c eb a7 a4 f6 85 f0 45 fc 7b 46 d7 3e 5e e3 8b a1 16 e5 59 e8 79
69 65 54 00 6c 89 ... >

Private Key is: <Buffer 30 82 01 cd 30 57 06 09 2a 86 48 86 f7 0d 01 05 0d 30
4a 30 29 06 09 2a 86 48 86 f7 0d 01 05 0c 30 1c 04 08 1e a2 b4 ea ce 50 0e 80
02 02 08 00 30 0c ... >

উদাহরণ

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

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

// Importing generateKeyPair from crypto module
const { generateKeyPair } = require('crypto');

// Calling generateKeyPair() method with the below parameters
generateKeyPair('ec', {
   namedCurve: 'secp256k1', // Options
   publicKeyEncoding: {
      type: 'spki',
      format: 'der'
   },
   privateKeyEncoding: {
      type: 'pkcs8',
      format: 'der'
   }
},(err, publicKey, privateKey) => { // Callback function
   if(!err)
   {
      // This will print the asymmetric key pair
      console.log("Public Key is: ", publicKey);
      console.log("Public Key in hex is: ", publicKey.toString('hex'));
      console.log();
      console.log("Private Key is: ", privateKey);
      console.log("Private Key in hex is: ",
      privateKey.toString('hex'));
   }else{
      // Prints error if any
      console.log("Errr is: ", err);
   }
});

আউটপুট

C:\home\node>> node generateKeyPair.js
Public Key is: <Buffer 30 56 30 10 06 07 2a 86 48 ce 3d 02 01 06 05 2b 81 04
00 0a 03 42 00 04 d1 d0 0b 7e f7 e3 3e cf d8 08 2a 20 a8 5e 52 be ca 56 29 42
c3 6b 9f d3 15 7c ... >
Public Key in hex is:
3056301006072a8648ce3d020106052b8104000a03420004d1d00b7ef7e33ecfd8082a20a85e52
beca562942c36b9fd3157cf98b03b41ecc4b4e13b4cd5cd35814383ee76afabaf794faf6f31bc8
c9f7007748f74767677c

Private Key is: <Buffer 30 81 84 02 01 00 30 10 06 07 2a 86 48 ce 3d 02 01 06
05 2b 81 04 00 0a 04 6d 30 6b 02 01 01 04 20 05 01 a1 d5 80 f7 65 56 6f a3 3a
05 d3 6d ec 82 ad ... >
Private Key in hex is:
308184020100301006072a8648ce3d020106052b8104000a046d306b02010104200501a1d580f7
65566fa33a05d36dec82ad590ff8697d182ddca6b881acfbadd1a14403420004d1d00b7ef7e33e
cfd8082a20a85e52beca562942c36b9fd3157cf98b03b41ecc4b4e13b4cd5cd35814383ee76afa
baf794faf6f31bc8c9f7007748f74767677c

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

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

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

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