Skip to main content
ECDH.convertKey - crypto - Node documentation
method ECDH.convertKey

Usage in Deno

import { ECDH } from "node:crypto";
ECDH.convertKey(
curve: string,
inputEncoding?: BinaryToTextEncoding,
outputEncoding?:
"latin1"
| "hex"
| "base64"
| "base64url"
,
format?:
"uncompressed"
| "compressed"
| "hybrid"
,
): Buffer | string

Converts the EC Diffie-Hellman public key specified by key and curve to the format specified by format. The format argument specifies point encoding and can be 'compressed', 'uncompressed' or 'hybrid'. The supplied key is interpreted using the specified inputEncoding, and the returned key is encoded using the specified outputEncoding.

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.

If format is not specified the point will be returned in 'uncompressed' format.

If the inputEncoding is not provided, key is expected to be a Buffer, TypedArray, or DataView.

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

Parameters

curve: string
optional
inputEncoding: BinaryToTextEncoding

The encoding of the key string.

optional
outputEncoding:
"latin1"
| "hex"
| "base64"
| "base64url"

The encoding of the return value.

optional
format:
"uncompressed"
| "compressed"
| "hybrid"
= 'uncompressed'

Return Type

Buffer | string