- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
crypto.getDiffieHellman() Method in Node.js
The crypto.createDiffieHellmanGroup() is used for creating a pre-determined DiffieHellmanGroup key exchange object. Some of the supported DiffieHellmanGroups are: modp1, modp2, modp5, modp 14, modp16, modp17 etc. The benefit of using this method is that the parties don't need to generate or exchange a group modulus thus saving processing time.
Syntax
crypto.getDiffieHelmmanGroup(groupName)
Parameters
The above parameters are described as below −
groupName – It takes the input for the group name. The input is of type 'string'.
Example
Create a file with name – getdiffieHellman.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below −
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);
Output
C:\home
ode>> node getDiffieHellman.js DiffieHellmanGroup { _handle: { verifyError: [Getter] }, verifyError: 0 } DiffieHellmanGroup { _handle: { verifyError: [Getter] }, verifyError: 0 } true
Example
Let's take a look at one more example.
// 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);
Output
C:\home
ode>> 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)
Advertisements