summaryrefslogtreecommitdiff
path: root/cli/tests/unit_node/crypto_cipher_test.ts
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2023-02-20 22:22:28 +0530
committerGitHub <noreply@github.com>2023-02-20 22:22:28 +0530
commitea7ca00c895c401af57a7201f3c41524333e7939 (patch)
treea238ea54a003111ab6c1b7b1cb14e0669cb4f7af /cli/tests/unit_node/crypto_cipher_test.ts
parenta16c11c5d10052c688ba4c2eca09fd1a225e395a (diff)
perf: use ops for node:crypto ciphers (#17819)
Towards #17809
Diffstat (limited to 'cli/tests/unit_node/crypto_cipher_test.ts')
-rw-r--r--cli/tests/unit_node/crypto_cipher_test.ts50
1 files changed, 50 insertions, 0 deletions
diff --git a/cli/tests/unit_node/crypto_cipher_test.ts b/cli/tests/unit_node/crypto_cipher_test.ts
new file mode 100644
index 000000000..e3bd7ca26
--- /dev/null
+++ b/cli/tests/unit_node/crypto_cipher_test.ts
@@ -0,0 +1,50 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+import crypto from "node:crypto";
+import { Buffer } from "node:buffer";
+import {
+ assertEquals,
+ assertThrows,
+} from "../../../test_util/std/testing/asserts.ts";
+
+const rsaPrivateKey = Deno.readTextFileSync(
+ new URL("./testdata/rsa_private.pem", import.meta.url),
+);
+const rsaPublicKey = Deno.readTextFileSync(
+ new URL("./testdata/rsa_public.pem", import.meta.url),
+);
+
+const input = new TextEncoder().encode("hello world");
+
+Deno.test({
+ name: "rsa public encrypt and private decrypt",
+ fn() {
+ const encrypted = crypto.publicEncrypt(Buffer.from(rsaPublicKey), input);
+ const decrypted = crypto.privateDecrypt(
+ Buffer.from(rsaPrivateKey),
+ Buffer.from(encrypted),
+ );
+ assertEquals(decrypted, input);
+ },
+});
+
+Deno.test({
+ name: "rsa private encrypt and private decrypt",
+ fn() {
+ const encrypted = crypto.privateEncrypt(rsaPrivateKey, input);
+ const decrypted = crypto.privateDecrypt(
+ rsaPrivateKey,
+ Buffer.from(encrypted),
+ );
+ assertEquals(decrypted, input);
+ },
+});
+
+Deno.test({
+ name: "rsa public decrypt fail",
+ fn() {
+ const encrypted = crypto.publicEncrypt(rsaPublicKey, input);
+ assertThrows(() =>
+ crypto.publicDecrypt(rsaPublicKey, Buffer.from(encrypted))
+ );
+ },
+});