Creates an Elliptic Curve Diffie-Hellman (ECDH) key exchange object using a predefined curve specified by the curveName string. Use getCurves to obtain a list of available curve names. On recent OpenSSL releases, openssl ecparam -list_curves will also display the name and description of each available elliptic curve.
function
crypto.createECDH
Referenced types
class ECDH
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 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
- otherPublicKey: ArrayBufferView): NonSharedBuffer;
Computes the shared secret using
otherPublicKeyas the other party's public key and returns the computed shared secret. The supplied key is interpreted using specifiedinputEncoding, and the returned secret is encoded using the specifiedoutputEncoding. If theinputEncodingis not provided,otherPublicKeyis expected to be aBuffer,TypedArray, orDataView.If
outputEncodingis given a string will be returned; otherwise aBufferis returned.ecdh.computeSecretwill throw anERR_CRYPTO_ECDH_INVALID_PUBLIC_KEYerror whenotherPublicKeylies outside of the elliptic curve. SinceotherPublicKeyis usually supplied from a remote user over an insecure network, be sure to handle this exception accordingly.otherPublicKey: string,): NonSharedBuffer;Computes the shared secret using
otherPublicKeyas the other party's public key and returns the computed shared secret. The supplied key is interpreted using specifiedinputEncoding, and the returned secret is encoded using the specifiedoutputEncoding. If theinputEncodingis not provided,otherPublicKeyis expected to be aBuffer,TypedArray, orDataView.If
outputEncodingis given a string will be returned; otherwise aBufferis returned.ecdh.computeSecretwill throw anERR_CRYPTO_ECDH_INVALID_PUBLIC_KEYerror whenotherPublicKeylies outside of the elliptic curve. SinceotherPublicKeyis usually supplied from a remote user over an insecure network, be sure to handle this exception accordingly.@param inputEncodingThe
encodingof theotherPublicKeystring.otherPublicKey: ArrayBufferView,): string;Computes the shared secret using
otherPublicKeyas the other party's public key and returns the computed shared secret. The supplied key is interpreted using specifiedinputEncoding, and the returned secret is encoded using the specifiedoutputEncoding. If theinputEncodingis not provided,otherPublicKeyis expected to be aBuffer,TypedArray, orDataView.If
outputEncodingis given a string will be returned; otherwise aBufferis returned.ecdh.computeSecretwill throw anERR_CRYPTO_ECDH_INVALID_PUBLIC_KEYerror whenotherPublicKeylies outside of the elliptic curve. SinceotherPublicKeyis usually supplied from a remote user over an insecure network, be sure to handle this exception accordingly.@param outputEncodingThe
encodingof the return value.otherPublicKey: string,): string;Computes the shared secret using
otherPublicKeyas the other party's public key and returns the computed shared secret. The supplied key is interpreted using specifiedinputEncoding, and the returned secret is encoded using the specifiedoutputEncoding. If theinputEncodingis not provided,otherPublicKeyis expected to be aBuffer,TypedArray, orDataView.If
outputEncodingis given a string will be returned; otherwise aBufferis returned.ecdh.computeSecretwill throw anERR_CRYPTO_ECDH_INVALID_PUBLIC_KEYerror whenotherPublicKeylies outside of the elliptic curve. SinceotherPublicKeyis usually supplied from a remote user over an insecure network, be sure to handle this exception accordingly.@param inputEncodingThe
encodingof theotherPublicKeystring.@param outputEncodingThe
encodingof the return value. Generates private and public EC Diffie-Hellman key values, and returns the public key in the specified
formatandencoding. This key should be transferred to the other party.The
formatargument specifies point encoding and can be'compressed'or'uncompressed'. Ifformatis not specified, the point will be returned in'uncompressed'format.If
encodingis provided a string is returned; otherwise aBufferis returned.): string;Generates private and public EC Diffie-Hellman key values, and returns the public key in the specified
formatandencoding. This key should be transferred to the other party.The
formatargument specifies point encoding and can be'compressed'or'uncompressed'. Ifformatis not specified, the point will be returned in'uncompressed'format.If
encodingis provided a string is returned; otherwise aBufferis returned.@param encodingThe
encodingof the return value.If
encodingis specified, a string is returned; otherwise aBufferis returned.@returnsThe EC Diffie-Hellman in the specified
encoding.): string;If
encodingis specified, a string is returned; otherwise aBufferis returned.@param encodingThe
encodingof the return value.@returnsThe EC Diffie-Hellman in the specified
encoding.- encoding?: null,): NonSharedBuffer;
The
formatargument specifies point encoding and can be'compressed'or'uncompressed'. Ifformatis not specified the point will be returned in'uncompressed'format.If
encodingis specified, a string is returned; otherwise aBufferis returned.@param encodingThe
encodingof the return value.@returnsThe EC Diffie-Hellman public key in the specified
encodingandformat.): string;The
formatargument specifies point encoding and can be'compressed'or'uncompressed'. Ifformatis not specified the point will be returned in'uncompressed'format.If
encodingis specified, a string is returned; otherwise aBufferis returned.@param encodingThe
encodingof the return value.@returnsThe EC Diffie-Hellman public key in the specified
encodingandformat. - privateKey: ArrayBufferView): void;
Sets the EC Diffie-Hellman private key. If
encodingis provided,privateKeyis expected to be a string; otherwiseprivateKeyis expected to be aBuffer,TypedArray, orDataView.If
privateKeyis not valid for the curve specified when theECDHobject was created, an error is thrown. Upon setting the private key, the associated public point (key) is also generated and set in theECDHobject.privateKey: string,): void;Sets the EC Diffie-Hellman private key. If
encodingis provided,privateKeyis expected to be a string; otherwiseprivateKeyis expected to be aBuffer,TypedArray, orDataView.If
privateKeyis not valid for the curve specified when theECDHobject was created, an error is thrown. Upon setting the private key, the associated public point (key) is also generated and set in theECDHobject.@param encodingThe
encodingof theprivateKeystring. - curve: string,outputEncoding?: 'latin1' | 'base64' | 'base64url' | 'hex',format?: 'uncompressed' | 'compressed' | 'hybrid'): string | NonSharedBuffer;
Converts the EC Diffie-Hellman public key specified by
keyandcurveto the format specified byformat. Theformatargument specifies point encoding and can be'compressed','uncompressed'or'hybrid'. The supplied key is interpreted using the specifiedinputEncoding, and the returned key is encoded using the specifiedoutputEncoding.Use getCurves to obtain a list of available curve names. On recent OpenSSL releases,
openssl ecparam -list_curveswill also display the name and description of each available elliptic curve.If
formatis not specified the point will be returned in'uncompressed'format.If the
inputEncodingis not provided,keyis expected to be aBuffer,TypedArray, orDataView.Example (uncompressing a key):
const { createECDH, ECDH, } = await import('node:crypto'); const ecdh = createECDH('secp256k1'); ecdh.generateKeys(); const compressedKey = ecdh.getPublicKey('hex', 'compressed'); const uncompressedKey = ECDH.convertKey(compressedKey, 'secp256k1', 'hex', 'hex', 'uncompressed'); // The converted key and the uncompressed public key should be the same console.log(uncompressedKey === ecdh.getPublicKey('hex'));@param inputEncodingThe
encodingof thekeystring.@param outputEncodingThe
encodingof the return value.