温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/reference/node/crypto/createDiffieHellman
点击访问原文链接

Node crypto.createDiffieHellman function | API Reference | Bun

Node crypto.createDiffieHellman function | API Reference | BunBuildDocsReferenceGuidesBlogDiscord/node:crypto/createDiffieHellmanFcreateDiffieHellman

Search the reference...

/

BuildDocsReferenceGuidesBlogDiscord/node:crypto/createDiffieHellmanFcreateDiffieHellman

function

crypto.createDiffieHellmanfunction createDiffieHellman(primeLength: number,generator?: number): DiffieHellman;

Creates a DiffieHellman key exchange object using the supplied prime and an optional specific generator.

The generator argument can be a number, string, or Buffer. If generator is not specified, the value 2 is used.

If primeEncoding is specified, prime is expected to be a string; otherwise a Buffer, TypedArray, or DataView is expected.

If generatorEncoding is specified, generator is expected to be a string; otherwise a number, Buffer, TypedArray, or DataView is expected.

function createDiffieHellman(prime: ArrayBuffer | ArrayBufferViewArrayBufferLike>,generator?: number | ArrayBuffer | ArrayBufferViewArrayBufferLike>): DiffieHellman;

Creates a DiffieHellman key exchange object using the supplied prime and an optional specific generator.

The generator argument can be a number, string, or Buffer. If generator is not specified, the value 2 is used.

If primeEncoding is specified, prime is expected to be a string; otherwise a Buffer, TypedArray, or DataView is expected.

If generatorEncoding is specified, generator is expected to be a string; otherwise a number, Buffer, TypedArray, or DataView is expected.

function createDiffieHellman(prime: ArrayBuffer | ArrayBufferViewArrayBufferLike>,generator: string,generatorEncoding: BinaryToTextEncoding): DiffieHellman;

Creates a DiffieHellman key exchange object using the supplied prime and an optional specific generator.

The generator argument can be a number, string, or Buffer. If generator is not specified, the value 2 is used.

If primeEncoding is specified, prime is expected to be a string; otherwise a Buffer, TypedArray, or DataView is expected.

If generatorEncoding is specified, generator is expected to be a string; otherwise a number, Buffer, TypedArray, or DataView is expected.

@param generatorEncoding

The encoding of the generator string.

function createDiffieHellman(prime: string,primeEncoding: BinaryToTextEncoding,generator?: number | ArrayBuffer | ArrayBufferViewArrayBufferLike>): DiffieHellman;

Creates a DiffieHellman key exchange object using the supplied prime and an optional specific generator.

The generator argument can be a number, string, or Buffer. If generator is not specified, the value 2 is used.

If primeEncoding is specified, prime is expected to be a string; otherwise a Buffer, TypedArray, or DataView is expected.

If generatorEncoding is specified, generator is expected to be a string; otherwise a number, Buffer, TypedArray, or DataView is expected.

@param primeEncoding

The encoding of the prime string.

function createDiffieHellman(prime: string,primeEncoding: BinaryToTextEncoding,generator: string,generatorEncoding: BinaryToTextEncoding): DiffieHellman;

Creates a DiffieHellman key exchange object using the supplied prime and an optional specific generator.

The generator argument can be a number, string, or Buffer. If generator is not specified, the value 2 is used.

If primeEncoding is specified, prime is expected to be a string; otherwise a Buffer, TypedArray, or DataView is expected.

If generatorEncoding is specified, generator is expected to be a string; otherwise a number, Buffer, TypedArray, or DataView is expected.

@param primeEncoding

The encoding of the prime string.

@param generatorEncoding

The encoding of the generator string.

Referenced typesclass 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 DiffieHellman object.

The following values are valid for this property (as defined in node:constants module):

DH_CHECK_P_NOT_SAFE_PRIMEDH_CHECK_P_NOT_PRIMEDH_UNABLE_TO_CHECK_GENERATORDH_NOT_SUITABLE_GENERATOR
computeSecret(otherPublicKey: ArrayBufferView,inputEncoding?: null,outputEncoding?: null): NonSharedBuffer;

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 the specified inputEncoding, and secret is encoded using specified outputEncoding. If the inputEncoding is not provided, otherPublicKey is expected to be a Buffer, TypedArray, or DataView.

If outputEncoding is given a string is returned; otherwise, a Buffer is returned.

@param inputEncoding

The encoding of an otherPublicKey string.

@param outputEncoding

The encoding of the return value.

computeSecret(otherPublicKey: string,inputEncoding: BinaryToTextEncoding,outputEncoding?: null): NonSharedBuffer;

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 the specified inputEncoding, and secret is encoded using specified outputEncoding. If the inputEncoding is not provided, otherPublicKey is expected to be a Buffer, TypedArray, or DataView.

If outputEncoding is given a string is returned; otherwise, a Buffer is returned.

@param inputEncoding

The encoding of an otherPublicKey string.

@param outputEncoding

The encoding of the return value.

computeSecret(otherPublicKey: ArrayBufferView,inputEncoding: null,outputEncoding: BinaryToTextEncoding): string;

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 the specified inputEncoding, and secret is encoded using specified outputEncoding. If the inputEncoding is not provided, otherPublicKey is expected to be a Buffer, TypedArray, or DataView.

If outputEncoding is given a string is returned; otherwise, a Buffer is returned.

@param inputEncoding

The encoding of an otherPublicKey string.

@param outputEncoding

The encoding of the return value.

computeSecret(otherPublicKey: string,inputEncoding: BinaryToTextEncoding,outputEncoding: BinaryToTextEncoding): string;

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 the specified inputEncoding, and secret is encoded using specified outputEncoding. If the inputEncoding is not provided, otherPublicKey is expected to be a Buffer, TypedArray, or DataView.

If outputEncoding is given a string is returned; otherwise, a Buffer is returned.

@param inputEncoding

The encoding of an otherPublicKey string.

@param outputEncoding

The encoding of the return value.

generateKeys(): NonSharedBuffer;

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. If encoding is provided a string is returned; otherwise a Buffer is 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 updates the public key but does not generate a new private key.

generateKeys(encoding: BinaryToTextEncoding): 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. If encoding is provided a string is returned; otherwise a Buffer is 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 updates the public key but does not generate a new private key.

@param encoding

The encoding of the return value.

getGenerator(): NonSharedBuffer;

Returns the Diffie-Hellman generator in the specified encoding. If encoding is provided a string is returned; otherwise a Buffer is returned.

getGenerator(encoding: BinaryToTextEncoding): string;

Returns the Diffie-Hellman generator in the specified encoding. If encoding is provided a string is returned; otherwise a Buffer is returned.

@param encoding

The encoding of the return value.

getPrime(): NonSharedBuffer;

Returns the Diffie-Hellman prime in the specified encoding. If encoding is provided a string is returned; otherwise a Buffer is returned.

getPrime(encoding: BinaryToTextEncoding): string;

Returns the Diffie-Hellman prime in the specified encoding. If encoding is provided a string is returned; otherwise a Buffer is returned.

@param encoding

The encoding of the return value.

getPrivateKey(): NonSharedBuffer;

Returns the Diffie-Hellman private key in the specified encoding. If encoding is provided a string is returned; otherwise a Buffer is returned.

getPrivateKey(encoding: BinaryToTextEncoding): string;

Returns the Diffie-Hellman private key in the specified encoding. If encoding is provided a string is returned; otherwise a Buffer is returned.

@param encoding

The encoding of the return value.

getPublicKey(): NonSharedBuffer;

Returns the Diffie-Hellman public key in the specified encoding. If encoding is provided a string is returned; otherwise a Buffer is returned.

getPublicKey(encoding: BinaryToTextEncoding): string;

Returns the Diffie-Hellman public key in the specified encoding. If encoding is provided a string is returned; otherwise a Buffer is returned.

@param encoding

The encoding of the return value.

setPrivateKey(privateKey: ArrayBufferView): void;

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.

This function does not automatically compute the associated public key. Either diffieHellman.setPublicKey() or diffieHellman.generateKeys() can be used to manually provide the public key or to automatically derive it.

setPrivateKey(privateKey: string,encoding: BufferEncoding): void;

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.

This function does not automatically compute the associated public key. Either diffieHellman.setPublicKey() or diffieHellman.generateKeys() can be used to manually provide the public key or to automatically derive it.

@param encoding

The encoding of the privateKey string.

setPublicKey(publicKey: ArrayBufferView): 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.

setPublicKey(publicKey: string,encoding: BufferEncoding): 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.

@param encoding

The encoding of the publicKey string.

class ArrayBuffer

Represents a raw buffer of binary data, which is used to store data for the different typed arrays. ArrayBuffers cannot be read from or written to directly, but can be passed to a typed array or DataView Object to interpret the raw buffer as needed.

readonly [Symbol.toStringTag]: stringreadonly byteLength: number

Read-only. The length of the ArrayBuffer (in bytes).

resize(newByteLength?: number): void;

Resizes the ArrayBuffer to the specified size (in bytes).

MDN

resize(byteLength: number): ArrayBuffer;

Resize an ArrayBuffer in-place.

slice(begin: number,end?: number): ArrayBuffer;

Returns a section of an ArrayBuffer.

transfer(newByteLength?: number): ArrayBuffer;

Creates a new ArrayBuffer with the same byte content as this buffer, then detaches this buffer.

MDN

transferToFixedLength(newByteLength?: number): ArrayBuffer;

Creates a new non-resizable ArrayBuffer with the same byte content as this buffer, then detaches this buffer.

MDN

type BinaryToTextEncoding = 'base64' | 'base64url' | 'hex' | 'binary'

Resources

ReferenceDocsGuidesDiscordMerch StoreGitHubBlog 

Toolkit

RuntimePackage managerTest runnerBundlerPackage runner

Project

Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicense

Baked with ❤️ in San Francisco

We're hiring →

Node crypto.createDiffieHellman function | API Reference | Bun,AI智能索引,全网链接索引,智能导航,网页索引

    API documentation for function node:crypto.createDiffieHellman | Bun