diff --git a/cli/tests/unit_node/crypto_cipher_test.ts b/cli/tests/unit_node/crypto_cipher_test.ts index b14d149f5e..3f740f40c5 100644 --- a/cli/tests/unit_node/crypto_cipher_test.ts +++ b/cli/tests/unit_node/crypto_cipher_test.ts @@ -1,6 +1,8 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. 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, @@ -89,6 +91,27 @@ Deno.test({ }, }); +Deno.test({ + name: "createCipheriv - transform stream", + async fn() { + const result = await buffer( + Readable.from("foo".repeat(15)).pipe(crypto.createCipheriv( + "aes-128-cbc", + new Uint8Array(16), + new Uint8Array(16), + )), + ); + // deno-fmt-ignore + assertEquals([...result], [ + 129, 19, 202, 142, 137, 51, 23, 53, 198, 33, + 214, 125, 17, 5, 128, 57, 162, 217, 220, 53, + 172, 51, 85, 113, 71, 250, 44, 156, 80, 4, + 158, 92, 185, 173, 67, 47, 255, 71, 78, 187, + 80, 206, 42, 5, 34, 104, 1, 54 + ]); + }, +}); + Deno.test({ name: "createDecipheriv - basic", fn() { @@ -110,3 +133,24 @@ Deno.test({ ); }, }); + +Deno.test({ + name: "createDecipheriv - transform stream", + async fn() { + const stream = Readable.from([ + // deno-fmt-ignore + new Uint8Array([ + 129, 19, 202, 142, 137, 51, 23, 53, 198, 33, + 214, 125, 17, 5, 128, 57, 162, 217, 220, 53, + 172, 51, 85, 113, 71, 250, 44, 156, 80, 4, + 158, 92, 185, 173, 67, 47, 255, 71, 78, 187, + 80, 206, 42, 5, 34, 104, 1, 54 + ]), + ]).pipe(crypto.createDecipheriv( + "aes-128-cbc", + new Uint8Array(16), + new Uint8Array(16), + )); + assertEquals(await text(stream), "foo".repeat(15)); + }, +}); diff --git a/ext/node/polyfills/internal/crypto/cipher.ts b/ext/node/polyfills/internal/crypto/cipher.ts index d40978cf6d..670c1bcce1 100644 --- a/ext/node/polyfills/internal/crypto/cipher.ts +++ b/ext/node/polyfills/internal/crypto/cipher.ts @@ -129,7 +129,17 @@ export class Cipheriv extends Transform implements Cipher { iv: BinaryLike | null, options?: TransformOptions, ) { - super(options); + super({ + transform(chunk, encoding, cb) { + this.push(this.update(chunk, encoding)); + cb(); + }, + final(cb) { + this.push(this.final()); + cb(); + }, + ...options, + }); this.#cache = new BlockModeCache(false); this.#context = ops.op_node_create_cipheriv(cipher, key, iv); } @@ -235,7 +245,17 @@ export class Decipheriv extends Transform implements Cipher { iv: BinaryLike | null, options?: TransformOptions, ) { - super(options); + super({ + transform(chunk, encoding, cb) { + this.push(this.update(chunk, encoding)); + cb(); + }, + final(cb) { + this.push(this.final()); + cb(); + }, + ...options, + }); this.#cache = new BlockModeCache(true); this.#context = ops.op_node_create_decipheriv(cipher, key, iv); }