ECDH

external class ECDH(source)

The ECDH class is a utility for creating Elliptic Curve Diffie-Hellman (ECDH) key exchanges.

Instances of the ECDH class can be created using the {@link createECDH} function.

import assert from 'node:assert';

const {
createECDH,
} = await import('node:crypto');

// Generate Alice's keys...
const alice = createECDH('secp521r1');
const aliceKey = alice.generateKeys();

// Generate Bob's keys...
const bob = createECDH('secp521r1');
const bobKey = bob.generateKeys();

// Exchange and generate the secret...
const aliceSecret = alice.computeSecret(bobKey);
const bobSecret = bob.computeSecret(aliceKey);

assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
// OK

Since

v0.11.14

Constructors

Link copied to clipboard
constructor()

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
fun computeSecret(otherPublicKey: ArrayBufferView<*>): Buffer

Computes the shared secret using otherPublicKey as the other party's public key and returns the computed shared secret. The supplied key is interpreted using specified inputEncoding, and the returned secret is encoded using the specified outputEncoding. If the inputEncoding is not provided, otherPublicKey is expected to be a Buffer, TypedArray, or DataView.

fun computeSecret(otherPublicKey: ArrayBufferView<*>, outputEncoding: BinaryToTextEncoding): String
fun computeSecret(otherPublicKey: String, inputEncoding: BinaryToTextEncoding): Buffer
fun computeSecret(otherPublicKey: String, inputEncoding: BinaryToTextEncoding, outputEncoding: BinaryToTextEncoding): String
Link copied to clipboard

Generates private and public EC Diffie-Hellman key values, and returns the public key in the specified format and encoding. This key should be transferred to the other party.

fun generateKeys(encoding: BinaryToTextEncoding, format: ECDHKeyFormat = definedExternally): String
Link copied to clipboard

If encoding is specified, a string is returned; otherwise a Buffer is returned.

Link copied to clipboard
fun getPublicKey(encoding: Nothing? = definedExternally, format: ECDHKeyFormat = definedExternally): Buffer

The format argument specifies point encoding and can be 'compressed' or 'uncompressed'. If format is not specified the point will be returned in'uncompressed' format.

fun getPublicKey(encoding: BinaryToTextEncoding, format: ECDHKeyFormat = definedExternally): String
Link copied to clipboard
fun setPrivateKey(privateKey: ArrayBufferView<*>)

Sets the EC Diffie-Hellman private key. If encoding is provided, privateKey is expected to be a string; otherwise privateKey is expected to be a Buffer, TypedArray, or DataView.

fun setPrivateKey(privateKey: String, encoding: BinaryToTextEncoding)