From 08e5606c3400d3a993c0ce6748901c56fc3db35b Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Fri, 5 Jul 2024 10:10:22 +0200 Subject: fix(ext/node): rewrite digest handling (#24392) Previously we had many different code paths all handling digests in different places, all with wildly different digest support. This commit rewrites this to use a single digest handling mechanism for all digest operations. It adds various aliases for digest algorithms, like node does. For example `sha1WithRSAEncryption` is an alias for `sha1`. It also adds support for `md5-sha1` digests in various places. --- tests/unit_node/crypto/generate_fixture.mjs | 69 +++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 tests/unit_node/crypto/generate_fixture.mjs (limited to 'tests/unit_node/crypto/generate_fixture.mjs') diff --git a/tests/unit_node/crypto/generate_fixture.mjs b/tests/unit_node/crypto/generate_fixture.mjs new file mode 100644 index 000000000..3724fe4af --- /dev/null +++ b/tests/unit_node/crypto/generate_fixture.mjs @@ -0,0 +1,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), +); -- cgit v1.2.3