crypto.createECDH() Method in Node.js


The crypto.createECDH() is used to create an elliptic curve also known as Elliptic Curve Diffie-Hellman i.e ECDH that uses a curve predefined by the input parameter curveName. You can use crypto.getCurves to get the list of all the available curve names. This method is part of the 'crypto' module.

Syntax

crypto.createECDH(curveName)

Parameters

The above parameters are described as below

  • curveName – It takes the input for the curve name. This curveName will deined the predefined curve for creating ECDH.

Example

Create a file with name – createECDH.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 createECDH.js

createECDH.js

 Live Demo

// A node demo program for creating the ECDH

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

// Calling the getCiphers() method
const curve = crypto.createECDH('secp521r1');

// Printing the curve keys...
console.log(curve.generateKeys());

Output

C:\home
ode>> node createECDH.js <Buffer 04 00 be c4 3b eb cc ea 33 84 31 b0 7d 8b 9f e6 5b e0 6e 3a 40 21 49 f0 20 9f 92 33 cf 32 d7 a7 f1 df 90 82 9b fe 8f 7b 98 5b 7d 1a ee c6 ae b1 bd 1a ... >

Example

Let's take a look at one more example.

 Live Demo

// A node demo program for creating the ECDH

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

// Calling the getCiphers() method
const curve = crypto.createECDH('secp521r1');
curve.generateKeys();

// Printing public & private curve keys...
console.log("Public Key: ", curve.getPublicKey());
console.log("Private Kye: ", curve.getPrivateKey());

Output

C:\home
ode>> node cipherUpdate.js Public Key: <Buffer 04 01 10 f7 fb d9 d7 f9 70 ba 6e 59 42 77 b6 1b 28 21 f1 3f ac 43 28 72 c6 33 b5 89 d3 77 6e 5a ea 8a 8a a1 27 a7 ab f1 b1 ea 41 ac dc c5 09 83 01 48 ... > Private Kye: <Buffer 01 d8 c4 d9 df 5c c8 54 e2 1f 82 94 ba 9c cd bc 88 3a e5 88 aa bd c8 2b 5c e9 f4 59 81 0b ae 18 f4 bf 21 43 56 74 55 d8 1d e6 b8 5f d8 e7 e2 52 ad 03 ... >

Updated on: 20-May-2021

266 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements