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 'assert';

const {
createDiffieHellman
} = await import('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'));

Hierarchy

  • DiffieHellman

Constructors

Properties

verifyError: number

A bit field containing any warnings and/or errors resulting from a check performed during initialization of the DiffieHellman object.

The following values are valid for this property (as defined in constantsmodule):

  • DH_CHECK_P_NOT_SAFE_PRIME
  • DH_CHECK_P_NOT_PRIME
  • DH_UNABLE_TO_CHECK_GENERATOR
  • DH_NOT_SUITABLE_GENERATOR

Methods

  • Generates private and public Diffie-Hellman key values, and returns the public key in the specified encoding. This key should be transferred to the other party. If encoding is provided a string is returned; otherwise a Buffer is returned.

    Returns "buffer".Buffer

  • Parameters

    Returns string

  • Sets the Diffie-Hellman private key. If the encoding argument is provided,privateKey is expected to be a string. If no encoding is provided, privateKey is expected to be a Buffer, TypedArray, or DataView.

    Parameters

    • privateKey: ArrayBufferView

    Returns void

  • Parameters

    • privateKey: string
    • encoding: BufferEncoding

    Returns void

  • Sets the Diffie-Hellman public key. If the encoding argument is provided,publicKey is expected to be a string. If no encoding is provided, publicKey is expected to be a Buffer, TypedArray, or DataView.

    Parameters

    • publicKey: ArrayBufferView

    Returns void

  • Parameters

    • publicKey: string
    • encoding: BufferEncoding

    Returns void

Generated using TypeDoc