Skip to content
Related Articles

Related Articles

Improve Article

Node.js ecdh.getPrivateKey() Method

  • Last Updated : 27 Jul, 2021

The ecdh.getPrivateKey() method is an inbuilt application programming interface of class ECDH within the crypto module which is used to get the private key of the Elliptic Curve Diffie-Hellman (ECDH) object. The encoding of the key can be specified using the encoding parameter and the format using the format parameter.

Note: The keys have to be first generated using the generateKeys() method before they can be retrieved using the private key.

Syntax:

ecdh.getPrivateKey( encoding )

Parameters: This method accepts a single parameter as mentioned above and described below:

  • encoding: This is a string value that specifies the encoding of the return value. It is an optional parameter.
  • format: It is a string that specifies the format of the keys. The value can be ‘compressed’ or ‘uncompressed’. It is an optional parameter.

Return Value: It returns the Elliptic Curve DiffieHellman private key in the specified encoding. When the encoding is not provided, it is returned as a Buffer, otherwise, a String is returned.



The examples below demonstrate this method:

Example 1:

Javascript




const crypto = require('crypto');
  
// Generate an ECDH object for geekA
const geekA = crypto.createECDH('secp521r1');
  
// Generate an ECDH object for geekB
const geekB = crypto.createECDH('secp521r1');
  
// Generate the keys for both the geeks
geekA.generateKeys();
geekB.generateKeys();
  
// Get the private key for geekA
const geekAprivateKey = geekA.getPrivateKey();
  
console.log("Private Key of Geek A is:",
  geekAprivateKey);
  
// Get the private key for geekB
const geekBprivateKey = geekB.getPrivateKey();
  
console.log("Private Key of Geek B is:",
  geekBprivateKey);

Output:

Private Key of Geek A is: <Buffer b1 cd 2f e7 52 b4 8b a9 fe 2d 1c d8 6d 35 d9 7b 2f 2b 48 27 40 ac 35 3a 34 3a 63 5b cd 28 d0 5f 38 9b d6 ad 6c d3 a7 6e ed d5 9e 84 e6 e6 83 84 c4 cb … 15 more bytes>
Private Key of Geek B is: <Buffer 01 2f b2 ea a9 65 23 27 65 be ab 68 3a 6e 88 6e db c4 6a 65 9d 52 b7 9e 69 39 b5 5f 3a 87 55 3d f6 62 51 94 5e 24 48 62 96 dc 3b 4f 10 d2 30 99 03 4c … 16 more bytes>

Example 2:

Javascript




const crypto = require('crypto');
  
// Generate an ECDH object for geekA
const geekA = crypto.createECDH('secp521r1');
  
// Generate an ECDH object for geekB
const geekB = crypto.createECDH('secp521r1');
  
// Generate the keys for both the geeks
geekA.generateKeys();
geekB.generateKeys();
  
// Get the private key for geekA in base64
const geekAPrivateKey =
  geekA.getPrivateKey('base64');
  
console.log("Private Key of Geek A is:",
  geekAPrivateKey);
  
// Get the private key for geekB in hex
const geekBPrivateKey =
  geekB.getPrivateKey('hex');
  
console.log("Private Key of Geek B is:",
  geekBPrivateKey);

Output:

Private Key of Geek A is: AXZvAlAJi4U5gL0Hj+kkriNlH45k+UkIz27MD0x4jsbKTjEUcU8vTUFvcw3aLHt4lit+V1Jp6fQ/4MH2dFNlfpu6
Private Key of Geek B is: 0152465d0c729f46f1f78532fde54eb0218b491e5a87926120862b79de9084e7a78f604de4d4d9f4c31ab7819bf6cf8f0d75849781ad556fed947ad968e008c90940

Reference: https://nodejs.org/api/crypto.html#crypto_ecdh_getprivatekey_encoding




My Personal Notes arrow_drop_up
Recommended Articles
Page :