From 1f60b8af97b15cb8e33f68c44f602cf69d79bd7a Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Thu, 21 Mar 2024 14:11:54 +0530 Subject: fix(ext/node): ECDH.publicKey() point encoding (#23013) --- ext/node/polyfills/internal/crypto/diffiehellman.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'ext/node/polyfills/internal/crypto/diffiehellman.ts') diff --git a/ext/node/polyfills/internal/crypto/diffiehellman.ts b/ext/node/polyfills/internal/crypto/diffiehellman.ts index da7907734..6058433ba 100644 --- a/ext/node/polyfills/internal/crypto/diffiehellman.ts +++ b/ext/node/polyfills/internal/crypto/diffiehellman.ts @@ -9,6 +9,7 @@ import { op_node_dh_generate2, op_node_ecdh_compute_public_key, op_node_ecdh_compute_secret, + op_node_ecdh_encode_pubkey, op_node_ecdh_generate_keys, op_node_gen_prime, } from "ext:core/ops"; @@ -1239,7 +1240,7 @@ export class ECDH { format: ECDHKeyFormat = "uncompressed", ): Buffer | string { this.#pubbuf = Buffer.alloc( - format.trim() == "compressed" + format == "compressed" ? this.#curve.publicKeySizeCompressed : this.#curve.publicKeySize, ); @@ -1269,12 +1270,17 @@ export class ECDH { getPublicKey(encoding: BinaryToTextEncoding, format?: ECDHKeyFormat): string; getPublicKey( encoding?: BinaryToTextEncoding, - _format?: ECDHKeyFormat, + format: ECDHKeyFormat = "uncompressed", ): Buffer | string { + const pubbuf = Buffer.from(op_node_ecdh_encode_pubkey( + this.#curve.name, + this.#pubbuf, + format == "compressed", + )); if (encoding !== undefined) { - return this.#pubbuf.toString(encoding); + return pubbuf.toString(encoding); } - return this.#pubbuf; + return pubbuf; } setPrivateKey(privateKey: ArrayBufferView): void; -- cgit v1.2.3