Crypto.createDiffieHellmanGroup() का उपयोग पूर्व-निर्धारित DiffieHellmanGroup कुंजी एक्सचेंज ऑब्जेक्ट बनाने के लिए किया जाता है। समर्थित DiffieHellmanGroups में से कुछ हैं:modp1, modp2, modp5, modp 14, modp16, modp17 आदि। इस पद्धति का उपयोग करने का लाभ यह है कि पार्टियों को समूह मापांक उत्पन्न करने या विनिमय करने की आवश्यकता नहीं होती है जिससे प्रसंस्करण समय की बचत होती है।
सिंटैक्स
crypto.getDiffieHelmmanGroup(groupName)
पैरामीटर
उपरोक्त पैरामीटर नीचे वर्णित हैं -
-
समूह का नाम - यह समूह के नाम के लिए इनपुट लेता है। इनपुट 'स्ट्रिंग' प्रकार का है।
उदाहरण
नाम के साथ एक फाइल बनाएं - getdiffieHellman.js और नीचे दिए गए कोड स्निपेट को कॉपी करें। फ़ाइल बनाने के बाद, इस कोड को चलाने के लिए निम्न कमांड का उपयोग करें जैसा कि नीचे दिए गए उदाहरण में दिखाया गया है -
node getDiffieHellman.js
getdiffieHellman.js
// crypto.getDiffieHellman() Demo Example
// Importing the crypto module
const crypto = require('crypto');
const server = crypto.getDiffieHellman('modp1');
const client = crypto.getDiffieHellman('modp1');
// Printing DiffieHellman values
console.log(server);
console.log(client);
// Generating public and private keys
server.generateKeys();
client.generateKeys();
// Gettong public key
const serverSecret = server.computeSecret(client.getPublicKey(), null, 'hex');
const clientSecret = client.computeSecret(server.getPublicKey(), null, 'hex');
/* aliceSecret and bobSecret should be the same */
console.log(serverSecret === clientSecret); आउटपुट
C:\home\node>> node getDiffieHellman.js
DiffieHellmanGroup { _handle: { verifyError: [Getter] }, verifyError: 0 }
DiffieHellmanGroup { _handle: { verifyError: [Getter] }, verifyError: 0 }
true उदाहरण
आइए एक और उदाहरण देखें।
// crypto.getDiffieHellman() Demo Example
// Importing the crypto module
const crypto = require('crypto');
const dh1 = crypto.getDiffieHellman('modp17');
const dh2 = crypto.getDiffieHellman('modp14');
// Generating public and private keys
dh1.generateKeys();
dh2.generateKeys();
// Gettong public key
const dh1Key = dh1.computeSecret(dh2.getPublicKey(), null, 'hex');
const dh2Key = dh2.computeSecret(dh1.getPublicKey(), null, 'hex');
/* aliceSecret and bobSecret should be the same */
console.log(dh1Key === dh2Key); आउटपुट
C:\home\node>> node getDiffieHellman.js internal/crypto/diffiehellman.js:102 const ret = this._handle.computeSecret(toBuf(key, inEnc)); ^ Error: Supplied key is too large at DiffieHellmanGroup.dhComputeSecret [as computeSecret] (internal/crypto/diffiehellman.js:102:28) at Object.<anonymous> (/home/node/test/getDiffieHellman .js:15:20) 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)