Skip to content
Related Articles

Related Articles

Improve Article

Node.js ecdh.getPublicKey() Method

  • Last Updated : 22 Jul, 2021

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

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

Syntax:

ecdh.getPublicKey( encoding, format )

Parameters: This method accepts two parameters as mentioned above and described below:

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

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



 

Example 1:The examples below demonstrate this method:

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 public key for geekA
const geekAPublicKey = geekA.getPublicKey();
  
console.log("Public Key of Geek A is:", geekAPublicKey);
  
// Generate keys for geekB
const geekBPublicKey = geekB.getPublicKey();
  
console.log("Public Key of Geek B is:", geekBPublicKey);

Output:

Public Key of Geek A is: <Buffer 04 00 47 18 d4 45 58 3c 8e 33 37 0d a4 ef 42 6a 06 a2 e6 bd a6 4d fe a7 97 e2 e7 55 ce 04 ae 60 71 cc f3 cc 94 83 0c 57 72 2d ad a2 95 ab 7b 97 62 77 … 83 more bytes>
Public Key of Geek B is: <Buffer 04 01 03 2c 43 12 21 39 d2 d5 e8 dc bf f7 c6 cf b2 a3 15 62 00 47 50 83 2e 22 ce 54 20 13 dd fa d2 0c 0f 31 fe b0 c5 1f af 65 a1 d0 b0 49 66 42 61 99 … 83 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 public key for geekA in base64
const geekAPublicKey = 
  geekA.getPublicKey('base64');
  
console.log(
  "Public Key of Geek A is:", geekAPublicKey);
  
// Generate keys for geekB in hex in
// the compressed format
const geekBPublicKey =
  geekB.getPublicKey('hex', 'compressed');
  
console.log(
  "Public Key of Geek B is:", geekBPublicKey);

Output:

Public Key of Geek A is: BAECc88Lu8XdEw2wUilULCKCcH0kQC79LilcYuJ92AuUICTIymP3cfStcD5GfkbF329l2wBZr4xwUgQXagyE6isnNAHMoXs2U4eo3NsCmSI2+BxSsJHYWKKim8nJQN8m7VGsQ4D0fpMGnwGFCjouO+V0vBB3ZVEkPfSWKjWytI0Wlf7kxA==
Public Key of Geek B is: 02015ed9d8734b8568583926cb1fa5423d8607458fe236bc92c8652c3beb8c6940e673379b944f39c5549a802eabbe8b6723220b9eaa22293fee76591b9fabbb29d539

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :