mirror of
https://github.com/denoland/deno.git
synced 2024-12-01 16:51:13 -05:00
74 lines
2 KiB
TypeScript
74 lines
2 KiB
TypeScript
// 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))
|
|
);
|
|
},
|
|
});
|
|
|
|
Deno.test({
|
|
name: "createCipheriv - basic",
|
|
fn() {
|
|
const cipher = crypto.createCipheriv(
|
|
"aes-128-cbc",
|
|
new Uint8Array(16),
|
|
new Uint8Array(16),
|
|
);
|
|
assertEquals(
|
|
cipher.update(new Uint8Array(16), undefined, "hex"),
|
|
"66e94bd4ef8a2c3b884cfa59ca342b2e",
|
|
);
|
|
assertEquals(
|
|
cipher.update(new Uint8Array(19), undefined, "hex"),
|
|
"f795bd4a52e29ed713d313fa20e98dbc",
|
|
);
|
|
assertEquals(
|
|
cipher.update(new Uint8Array(55), undefined, "hex"),
|
|
"a10cf66d0fddf3405370b4bf8df5bfb347c78395e0d8ae2194da0a90abc9888a94ee48f6c78fcd518a941c3896102cb1",
|
|
);
|
|
assertEquals(cipher.final("hex"), "e11901dde4a2f99fe4efc707e48c6aed");
|
|
},
|
|
});
|