1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-20 22:34:46 -05:00
denoland-deno/tests/unit_node/crypto/crypto_import_export.ts

32 lines
1 KiB
TypeScript
Raw Normal View History

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import crypto, { KeyFormat } from "node:crypto";
import path from "node:path";
import { Buffer } from "node:buffer";
import { assert } from "@std/assert/mod.ts";
import asymmetric from "./testdata/asymmetric.json" with { type: "json" };
Deno.test("crypto.createPrivateKey", async (t) => {
for (const key of asymmetric) {
await testCreatePrivateKey(t, key.name, "pem", "pkcs8");
await testCreatePrivateKey(t, key.name, "der", "pkcs8");
}
});
function testCreatePrivateKey(
t: Deno.TestContext,
name: string,
format: KeyFormat,
type: "pkcs8" | "pkcs1" | "sec1",
) {
if (name.includes("dh")) return;
return t.step(`crypto.createPrivateKey ${name} ${format} ${type}`, () => {
const file = path.join(
"./tests/unit_node/crypto/testdata/asymmetric",
`${name}.${type}.${format}`,
);
const key = Buffer.from(Deno.readFileSync(file));
const privateKey = crypto.createPrivateKey({ key, format, type });
assert(privateKey);
});
}