summaryrefslogtreecommitdiff
path: root/tests/unit_node/crypto/generate_fixture.mjs
blob: 3724fe4aff76abf9546251d9d21b3d0324c5ecbf (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// Run this file with `node` to regenerate the testdata/crypto_digest_fixtures.json file.

import { readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import crypto from "node:crypto";
import { Buffer } from "node:buffer";

const privateKey = readFileSync(
  join(import.meta.dirname, "..", "testdata", "rsa_private.pem"),
);

const fixtures = [];

const DATA = "Hello, world!";
const SALT = "salt";
const INFO = "info";
const ITERATIONS = 1000;
const KEY_LEN = 64;

for (const digest of crypto.getHashes()) {
  const hasher = crypto.createHash(digest);
  hasher.update(DATA);
  let hash;
  try {
    hash = hasher.digest().toString("hex");
  } catch {
    hash = null;
  }

  const sign = crypto.createSign(digest);
  sign.update(DATA);
  let signature;
  try {
    signature = sign.sign(privateKey).toString("hex");
  } catch {
    signature = null;
  }

  let pkdf2;
  try {
    pkdf2 = crypto.pbkdf2Sync(DATA, SALT, ITERATIONS, KEY_LEN, digest).toString(
      "hex",
    );
  } catch {
    pkdf2 = null;
  }

  let hkdf;
  try {
    hkdf = Buffer.from(crypto.hkdfSync(digest, DATA, SALT, INFO, KEY_LEN))
      .toString("hex");
  } catch {
    hkdf = null;
  }

  fixtures.push({
    digest,
    hash,
    signature,
    pkdf2,
    hkdf,
  });
}

writeFileSync(
  join(import.meta.dirname, "..", "testdata", "crypto_digest_fixtures.json"),
  JSON.stringify(fixtures, null, 2),
);