fix(ext/node): return Buffer from crypto cipher APIs (#28826)

Fixes https://github.com/denoland/deno/issues/28633
This commit is contained in:
Divy Srivastava 2025-04-10 01:01:30 -07:00 committed by GitHub
parent 447b5038c0
commit b26c30d938
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 5 deletions

View file

@ -438,7 +438,7 @@ export function privateEncrypt(
const padding = privateKey.padding || 1;
buffer = getArrayBufferOrView(buffer, "buffer");
return op_node_private_encrypt(data, buffer, padding);
return Buffer.from(op_node_private_encrypt(data, buffer, padding));
}
export function privateDecrypt(
@ -449,7 +449,7 @@ export function privateDecrypt(
const padding = privateKey.padding || 1;
buffer = getArrayBufferOrView(buffer, "buffer");
return op_node_private_decrypt(data, buffer, padding);
return Buffer.from(op_node_private_decrypt(data, buffer, padding));
}
export function publicEncrypt(
@ -460,7 +460,7 @@ export function publicEncrypt(
const padding = publicKey.padding || 1;
buffer = getArrayBufferOrView(buffer, "buffer");
return op_node_public_encrypt(data, buffer, padding);
return Buffer.from(op_node_public_encrypt(data, buffer, padding));
}
export function prepareKey(key) {

View file

@ -3,7 +3,7 @@ import crypto from "node:crypto";
import { Buffer } from "node:buffer";
import { Readable } from "node:stream";
import { buffer, text } from "node:stream/consumers";
import { assertEquals, assertThrows } from "@std/assert";
import { assert, assertEquals, assertThrows } from "@std/assert";
const rsaPrivateKey = Deno.readTextFileSync(
new URL("../testdata/rsa_private.pem", import.meta.url),
@ -12,7 +12,7 @@ const rsaPublicKey = Deno.readTextFileSync(
new URL("../testdata/rsa_public.pem", import.meta.url),
);
const input = new TextEncoder().encode("hello world");
const input = Buffer.from("hello world", "utf-8");
function zeros(length: number): Uint8Array {
return new Uint8Array(length);
@ -26,6 +26,8 @@ Deno.test({
Buffer.from(rsaPrivateKey),
Buffer.from(encrypted),
);
assert(Buffer.isBuffer(encrypted));
assert(Buffer.isBuffer(decrypted));
assertEquals(decrypted, input);
},
});
@ -49,10 +51,12 @@ Deno.test({
name: "rsa private encrypt and private decrypt",
fn() {
const encrypted = crypto.privateEncrypt(rsaPrivateKey, input);
assert(Buffer.isBuffer(encrypted));
const decrypted = crypto.privateDecrypt(
rsaPrivateKey,
Buffer.from(encrypted),
);
assert(Buffer.isBuffer(decrypted));
assertEquals(decrypted, input);
},
});
@ -61,6 +65,7 @@ Deno.test({
name: "rsa public decrypt fail",
fn() {
const encrypted = crypto.publicEncrypt(rsaPublicKey, input);
assert(Buffer.isBuffer(encrypted));
assertThrows(() =>
crypto.publicDecrypt(rsaPublicKey, Buffer.from(encrypted))
);