Open In App

Node.js ecdh.setPublicKey() Method

Last Updated : 23 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

This method is not usually required as the generateKeys() and the setPrivateKey() methods can be used to when computing the shared secret is needed in an application. An error would be thrown if the public key is not valid for the specified curve.

Syntax:

ecdh.setPublicKey( publicKey, encoding )

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

  • publicKey: This is the public key that needs to be set. It can be given in the format of a string, ArrayBuffer, Buffer, TypedArray or DataView.
  • encoding: This is a string value that specifies the encoding of the returned value. It is an optional parameter.

The examples below demonstrate this method:

Example 1: 

Javascript




const crypto = require('crypto');
 
// Generate an ECDH object for geekOne
const geekOne = crypto.createECDH('secp521r1');
 
// Generate a temporary ECDH object
// for generating a public key
const tmpECDH = crypto.createECDH('secp521r1');
 
// Get a temporary public key as a
// Buffer for demonstration
let tempECDHPublicKey = tmpECDH.generateKeys();
 
// Set the public key to be equal to the
// above generated key in the Buffer format
geekOne.setPublicKey(tempECDHPublicKey);
 
// Get the public key that was set
let geekOnePublicKey = geekOne.getPublicKey();
 
console.log("Public Key of Geek A is:",
  geekOnePublicKey);


Output: 

Public Key of Geek One is: <Buffer 04 01 83 85 b9 13 fd 2b 2b 7a bd 9d 8e 72 d0 bb 95 ec 24 73 51 e5 4d f1 00 68 07 7d 45 5b 37 5c 02 f6 03 56 4e 99 43 5b ba 44 57 4c e9 59 c9 ef b9 f4 … 83 more bytes>

Example 2: In this example, the key generated is a different curve. Therefore when this key is set using this method, it throws an error as it does not match the curve of the ECDH object. 

Javascript




const crypto = require('crypto');
 
// Generate an ECDH object for geekOne
const geekOne = crypto.createECDH('secp521r1');
 
// Generate a key with the encryption
// type as SHA256
let hashObj = crypto.createHash('sha256');
let tempPublicKey =
  hashObj.update('thisisapublickey', 'utf8').digest();
 
// Display the generated key
console.log("The generated key is:", tempPublicKey);
 
// Attempt to set the public key to
// be equal to the above generated key
geekOne.setPublicKey(tempPublicKey);


Output: 

The generated key is: <Buffer a7 de ff 8e 06 ff 75 5d 3e a0 87 61 8c 92 e1 eb 5d fe 9d d6 6c 27 7d 15 30 6e a7 a6 55 b1 03 02> 
node:internal/crypto/diffiehellman:231 
this[kHandle].setPublicKey(key); 
^
Error: Failed to convert Buffer to EC_POINT 
at ECDH.setPublicKey (node:internal/crypto/diffiehellman:231:17) 
at Object.<anonymous> (G:\tutorials\ecdh-setPublicKey\ex2.js:16:9) 
at Module._compile (node:internal/modules/cjs/loader:1095:14) 
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10) 
at Module.load (node:internal/modules/cjs/loader:975:32) 
at Function.Module._load (node:internal/modules/cjs/loader:816:12) 
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12) 
at node:internal/main/run_main_module:17:47 { 
code: ‘ERR_CRYPTO_OPERATION_FAILED’ 
}

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

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads