class
crypto.DiffieHellman
class DiffieHellman
The DiffieHellman class is a utility for creating Diffie-Hellman key exchanges.
Instances of the DiffieHellman class can be created using the createDiffieHellman function.
import assert from 'node:assert';
const {
createDiffieHellman,
} = await import('node:crypto');
// Generate Alice's keys...
const alice = createDiffieHellman(2048);
const aliceKey = alice.generateKeys();
// Generate Bob's keys...
const bob = createDiffieHellman(alice.getPrime(), alice.getGenerator());
const bobKey = bob.generateKeys();
// Exchange and generate the secret...
const aliceSecret = alice.computeSecret(bobKey);
const bobSecret = bob.computeSecret(aliceKey);
// OK
assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));- verifyError: number
A bit field containing any warnings and/or errors resulting from a check performed during initialization of the
DiffieHellmanobject.The following values are valid for this property (as defined in
node:constantsmodule):DH_CHECK_P_NOT_SAFE_PRIMEDH_CHECK_P_NOT_PRIMEDH_UNABLE_TO_CHECK_GENERATORDH_NOT_SUITABLE_GENERATOR
- inputEncoding?: BufferEncoding): 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 the specifiedinputEncoding, and secret is encoded using specifiedoutputEncoding. If theinputEncodingis not provided,otherPublicKeyis expected to be aBuffer,TypedArray, orDataView.If
outputEncodingis given a string is returned; otherwise, aBufferis returned.@param inputEncodingThe
encodingof anotherPublicKeystring.inputEncoding: undefined | null | BufferEncoding,outputEncoding: BufferEncoding): 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 the specifiedinputEncoding, and secret is encoded using specifiedoutputEncoding. If theinputEncodingis not provided,otherPublicKeyis expected to be aBuffer,TypedArray, orDataView.If
outputEncodingis given a string is returned; otherwise, aBufferis returned.@param inputEncodingThe
encodingof anotherPublicKeystring.@param outputEncodingThe
encodingof the return value. Generates private and public Diffie-Hellman key values unless they have been generated or computed already, and returns the public key in the specified
encoding. This key should be transferred to the other party. Ifencodingis provided a string is returned; otherwise aBufferis returned.This function is a thin wrapper around
DH_generate_key(). In particular, once a private key has been generated or set, calling this function only recomputes the public key from the existing private key. Since the public key is determined by the private key, the result will be the same unless the private key has been changed viadiffieHellman.setPrivateKey().encoding: BufferEncoding): string;Generates private and public Diffie-Hellman key values unless they have been generated or computed already, and returns the public key in the specified
encoding. This key should be transferred to the other party. Ifencodingis provided a string is returned; otherwise aBufferis returned.This function is a thin wrapper around
DH_generate_key(). In particular, once a private key has been generated or set, calling this function only recomputes the public key from the existing private key. Since the public key is determined by the private key, the result will be the same unless the private key has been changed viadiffieHellman.setPrivateKey().@param encodingThe
encodingof the return value.Returns the Diffie-Hellman generator in the specified
encoding. Ifencodingis provided a string is returned; otherwise aBufferis returned.encoding: BufferEncoding): string;Returns the Diffie-Hellman generator in the specified
encoding. Ifencodingis provided a string is returned; otherwise aBufferis returned.@param encodingThe
encodingof the return value.Returns the Diffie-Hellman prime in the specified
encoding. Ifencodingis provided a string is returned; otherwise aBufferis returned.encoding: BufferEncoding): string;Returns the Diffie-Hellman prime in the specified
encoding. Ifencodingis provided a string is returned; otherwise aBufferis returned.@param encodingThe
encodingof the return value.Returns the Diffie-Hellman private key in the specified
encoding. Ifencodingis provided a string is returned; otherwise aBufferis returned.encoding: BufferEncoding): string;Returns the Diffie-Hellman private key in the specified
encoding. Ifencodingis provided a string is returned; otherwise aBufferis returned.@param encodingThe
encodingof the return value.Returns the Diffie-Hellman public key in the specified
encoding. Ifencodingis provided a string is returned; otherwise aBufferis returned.encoding: BufferEncoding): string;Returns the Diffie-Hellman public key in the specified
encoding. Ifencodingis provided a string is returned; otherwise aBufferis returned.@param encodingThe
encodingof the return value.- encoding?: BufferEncoding): void;
Sets the Diffie-Hellman private key. If the
encodingargument is provided,privateKeyis expected to be a string. If noencodingis provided,privateKeyis expected to be aBuffer,TypedArray, orDataView.This function does not automatically compute the associated public key. Either
diffieHellman.setPublicKey()ordiffieHellman.generateKeys()can be used to manually provide the public key or to automatically derive it.@param encodingThe
encodingof theprivateKeystring. - encoding?: BufferEncoding): void;
Sets the Diffie-Hellman public key. If the
encodingargument is provided,publicKeyis expected to be a string. If noencodingis provided,publicKeyis expected to be aBuffer,TypedArray, orDataView.@param encodingThe
encodingof thepublicKeystring.