Node.js crypto.createDiffieHellman(primeLength, generator) Method
Last Updated :
31 Mar, 2023
The crypto.createDiffieHellman() method is used to create a Diffie-Hellman key exchange object. Also, creates prime of primeLength bits with the help of an optional specific numeric generator. Moreover, if the generator is not defined, then the value 2 is used.
Syntax:
crypto.createDiffieHellman( primeLength, generator )
Parameters: This method accepts two parameters as mentioned above and described below:
- primeLength: This parameter holds a number of primeLength.
- generator: It can hold a number, string, Buffer, TypedArray, or DataView type of data. Its default value is 2.
Return Value: It returns Diffie-Hellman key exchange object.
The below examples illustrate the use of crypto.createDiffieHellman() method in Node.js:
Example 1:
javascript
const crypto = require( 'crypto' );
const prime_length = 60;
let diffHell = crypto.createDiffieHellman(prime_length);
console.log(diffHell.generateKeys( 'base64' ));
|
Output:
CoWIWpiwbCE=
Example 2:
javascript
const crypto = require( 'crypto' );
const prime_length = 21;
const generator = 12;
let diffHell = crypto.createDiffieHellman(
prime_length, generator);
console.log(diffHell.generateKeys( 'hex' ));
console.log( "Public Key : " ,
diffHell.getPublicKey( 'base64' ));
console.log( "Private Key : " ,
diffHell.getPrivateKey( 'base64' ));
|
Output:
086501
Public Key : CGUB
Private Key : C1rL
Reference: https://nodejs.org/api/crypto.html#crypto_crypto_creatediffiehellman_primelength_generator
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...