summaryrefslogtreecommitdiff
path: root/tests/unit_node/crypto/crypto_import_export.ts
blob: fc41cbacc29764116b2bb3b08972d908cd17c632 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 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);
  });
}