diff options
author | Yoshiya Hinosawa <stibium121@gmail.com> | 2024-09-11 19:24:17 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-11 19:24:17 +0900 |
commit | aae3a6bcb41fdb305d61a95f176be0db1513c56e (patch) | |
tree | bd181542cf2f5b608ce0ed25bb9d3228b2a71644 | |
parent | 200145a09a51e96298f1ecd5ce78f75c27c7880a (diff) |
fix(ext/node): fix `Cipheriv#update(string, undefined)` (#25571)
-rw-r--r-- | ext/node/polyfills/internal/crypto/cipher.ts | 4 | ||||
-rw-r--r-- | tests/unit_node/crypto/crypto_cipher_test.ts | 36 |
2 files changed, 28 insertions, 12 deletions
diff --git a/ext/node/polyfills/internal/crypto/cipher.ts b/ext/node/polyfills/internal/crypto/cipher.ts index 2141edc76..a1449cd87 100644 --- a/ext/node/polyfills/internal/crypto/cipher.ts +++ b/ext/node/polyfills/internal/crypto/cipher.ts @@ -242,7 +242,7 @@ export class Cipheriv extends Transform implements Cipher { ): Buffer | string { // TODO(kt3k): throw ERR_INVALID_ARG_TYPE if data is not string, Buffer, or ArrayBufferView let buf = data; - if (typeof data === "string" && typeof inputEncoding === "string") { + if (typeof data === "string") { buf = Buffer.from(data, inputEncoding); } @@ -396,7 +396,7 @@ export class Decipheriv extends Transform implements Cipher { ): Buffer | string { // TODO(kt3k): throw ERR_INVALID_ARG_TYPE if data is not string, Buffer, or ArrayBufferView let buf = data; - if (typeof data === "string" && typeof inputEncoding === "string") { + if (typeof data === "string") { buf = Buffer.from(data, inputEncoding); } diff --git a/tests/unit_node/crypto/crypto_cipher_test.ts b/tests/unit_node/crypto/crypto_cipher_test.ts index ad424e5d4..20b9d052f 100644 --- a/tests/unit_node/crypto/crypto_cipher_test.ts +++ b/tests/unit_node/crypto/crypto_cipher_test.ts @@ -139,16 +139,32 @@ Deno.test({ Deno.test({ name: "createCipheriv - input encoding", fn() { - const cipher = crypto.createCipheriv( - "aes-128-cbc", - new Uint8Array(16), - new Uint8Array(16), - ); - assertEquals( - cipher.update("hello, world! hello, world!", "utf-8", "hex"), - "ca7df4d74f51b77a7440ead38343ab0f", - ); - assertEquals(cipher.final("hex"), "d0da733dec1fa61125c80a6f97e6166e"); + { + const cipher = crypto.createCipheriv( + "aes-128-cbc", + new Uint8Array(16), + new Uint8Array(16), + ); + assertEquals( + cipher.update("hello, world! hello, world!", "utf-8", "hex"), + "ca7df4d74f51b77a7440ead38343ab0f", + ); + assertEquals(cipher.final("hex"), "d0da733dec1fa61125c80a6f97e6166e"); + } + + { + const cipher = crypto.createCipheriv( + "aes-128-cbc", + new Uint8Array(16), + new Uint8Array(16), + ); + // update with string without input encoding + assertEquals( + cipher.update("hello, world! hello, world!").toString("hex"), + "ca7df4d74f51b77a7440ead38343ab0f", + ); + assertEquals(cipher.final("hex"), "d0da733dec1fa61125c80a6f97e6166e"); + } }, }); |