0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-29 08:58:01 -04:00

fix(ext/node): make cipher/decipher transform stream (#18408)

This commit is contained in:
Yoshiya Hinosawa 2023-03-24 22:29:14 +09:00 committed by GitHub
parent 94ef428b56
commit 3d75fb2be7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 2 deletions

View file

@ -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));
},
});

View file

@ -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);
}