crypto.createDiffieHellmanGroup() Method in Node.js

The crypto.createDiffieHellmanGroup() method creates a predefined Diffie-Hellman group using well-known parameters. This is an alias for crypto.getDiffieHellman() and provides a secure way to establish shared secrets between parties.

Syntax

crypto.createDiffieHellmanGroup(name)

Parameters

  • name ? A string specifying the predefined group name (e.g., 'modp1', 'modp2', 'modp5', 'modp14', 'modp15', 'modp16', 'modp17', 'modp18').

Return Value

Returns a DiffieHellman object that can generate keys and compute shared secrets.

Example 1: Basic Usage with modp1

Create a file with name ? diffieHellmanGroup.js and copy the below code snippet. After creating file, use the following command to run this code:

node diffieHellmanGroup.js

diffieHellmanGroup.js

// crypto.createDiffieHellmanGroup Demo Example

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

// Defining the group name
const name = 'modp1';

// Creating DiffieHellman group
var diffieHellmanGroup = crypto.createDiffieHellmanGroup(name);

// Displaying the encoded keys for the above group
console.log(diffieHellmanGroup.generateKeys('hex'));
55963b8969c6b5d67dc7a2d80e6bcbea55bdb50000661c8d6e026be5366619edb9971755942fd8
fa5cba5d66a0326db9957d0ff412bfe22ee2431398206b97210b86c802e1be6777bce3d6cf735f
bb3c7cdee4016db9d252a66be82d58158a21

Example 2: Using modp17 with Base64 Encoding

// crypto.createDiffieHellmanGroup Demo Example

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

// Defining the group name
const name = 'modp17';

// Creating DiffieHellman group
var diffieHellmanGroup = crypto.createDiffieHellmanGroup(name);

// Displaying the encoded keys for the above group
console.log('Public Key:', diffieHellmanGroup.generateKeys('base64'));
console.log('Prime:', diffieHellmanGroup.getPrime('hex'));
Public Key: p3PwqFTpIqOIE3mP+iaMHEosAy8jPCdRDck1HtvbFaMMvJzlfihwzLZB0ZCoEzDRSnlHo+0NjOkNLM
0jM0usS8Ri5wwxw19qwjr00HvxGf0crQtd8ytIVLFX2WWXaT7d8XR/vtw1BUgUVg0mNhKGTLDcVy1A
7CRHzCrDqePaVqnU0QkR2t7S1BvmaMXvkToAdzZnJMPSjZWPVOHzMTOTnpI450TQMLFLv/YGUmKbAO
BaAVbuwfWOHVQrcxgThGGtMRJjjayh0Q77RmoqpKcMWceJPlQhFIMG/jFrNEaacbgb0Ctnm1/d1UgE
UTIg0phE4/1GXdnEDRPGVXURaxzPU+N48QVAB7Dyzpcq4k2f/mARLILPufEP/8FBawmMDoyAE4PS/Z
1RFwwuInhdbhgxTTRV+nkzSwTt0V51q+B9fek2xDfeWDqwibr+OWdU0DXs07mUsFVJdVJSZFM7txIg
Prime: FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B2
2514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6
F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC
2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9E
D529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2
783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183560CCB6B69532F3B011CCF

Common Group Names

The method supports several predefined groups:

  • 'modp1' - 768 bits (deprecated, weak)
  • 'modp2' - 1024 bits
  • 'modp5' - 1536 bits
  • 'modp14' - 2048 bits (recommended)
  • 'modp15' - 3072 bits
  • 'modp16' - 4096 bits
  • 'modp17' - 6144 bits
  • 'modp18' - 8192 bits

Key Points

  • Use modp14 or higher for production applications
  • The method creates standardized groups defined in RFC specifications
  • Generated keys can be encoded in 'hex', 'base64', or 'binary' format
  • This is an alias for crypto.getDiffieHellman()

Conclusion

The crypto.createDiffieHellmanGroup() method provides a convenient way to create secure Diffie-Hellman key exchange using predefined parameters. Use groups with at least 2048 bits (modp14 or higher) for modern security requirements.

Updated on: 2026-03-15T23:19:00+05:30

194 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements