node/benchmark/crypto/create-keyobject.js
Filip Skokan f4413175b4
Some checks failed
Coverage Windows / coverage-windows (push) Waiting to run
Coverage Linux / coverage-linux (push) Failing after 1m29s
Coverage Linux (without intl) / coverage-linux-without-intl (push) Failing after 1m29s
Linters / lint-addon-docs (push) Successful in 2m20s
Linters / lint-cpp (push) Failing after 2m13s
Linters / format-cpp (push) Has been skipped
Test and upload documentation to artifacts / build-docs (push) Failing after 4m32s
Linters / lint-py (push) Failing after 2m18s
Linters / lint-yaml (push) Failing after 1m53s
Linters / lint-sh (push) Failing after 1m29s
Linters / lint-codeowners (push) Failing after 56s
Linters / lint-pr-url (push) Has been skipped
Linters / lint-readme (push) Successful in 1m26s
Notify on Push / Notify on Force Push on `main` (push) Has been skipped
Notify on Push / Notify on Push on `main` that lacks metadata (push) Has been skipped
Scorecard supply-chain security / Scorecard analysis (push) Failing after 53s
Linters / lint-js-and-md (push) Successful in 9m53s
benchmark: reflect current OpenSSL in crypto key benchmarks
PR-URL: https://github.com/nodejs/node/pull/59459
Reviewed-By: Richard Lau <richard.lau@ibm.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
2025-08-15 10:24:46 +00:00

90 lines
2.5 KiB
JavaScript

'use strict';
const common = require('../common.js');
const { hasOpenSSL } = require('../../test/common/crypto.js');
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const fixtures_keydir = path.resolve(__dirname, '../../test/fixtures/keys/');
function readKey(name) {
return fs.readFileSync(`${fixtures_keydir}/${name}.pem`, 'utf8');
}
function readKeyPair(publicKeyName, privateKeyName) {
return {
publicKey: readKey(publicKeyName),
privateKey: readKey(privateKeyName),
};
}
const keyFixtures = {
'ec': readKeyPair('ec_p256_public', 'ec_p256_private'),
'rsa': readKeyPair('rsa_public_2048', 'rsa_private_2048'),
'ed25519': readKeyPair('ed25519_public', 'ed25519_private'),
};
if (hasOpenSSL(3, 5)) {
keyFixtures['ml-dsa-44'] = readKeyPair('ml_dsa_44_public', 'ml_dsa_44_private');
}
const bench = common.createBenchmark(main, {
keyType: Object.keys(keyFixtures),
keyFormat: ['pkcs8', 'spki', 'der-pkcs8', 'der-spki', 'jwk-public', 'jwk-private'],
n: [1e3],
});
function measure(n, fn, input) {
bench.start();
for (let i = 0; i < n; ++i) {
fn(input);
}
bench.end(n);
}
function main({ n, keyFormat, keyType }) {
const keyPair = {
publicKey: crypto.createPublicKey(keyFixtures[keyType].publicKey),
privateKey: crypto.createPrivateKey(keyFixtures[keyType].privateKey),
};
let key, fn;
switch (keyFormat) {
case 'spki':
key = keyPair.publicKey.export({ format: 'pem', type: 'spki' });
fn = crypto.createPublicKey;
break;
case 'pkcs8':
key = keyPair.privateKey.export({ format: 'pem', type: 'pkcs8' });
fn = crypto.createPrivateKey;
break;
case 'der-spki': {
const options = { format: 'der', type: 'spki' };
key = { ...options, key: keyPair.publicKey.export(options) };
fn = crypto.createPublicKey;
break;
}
case 'der-pkcs8': {
const options = { format: 'der', type: 'pkcs8' };
key = { ...options, key: keyPair.privateKey.export(options) };
fn = crypto.createPrivateKey;
break;
}
case 'jwk-public': {
const options = { format: 'jwk' };
key = { ...options, key: keyPair.publicKey.export(options) };
fn = crypto.createPublicKey;
break;
}
case 'jwk-private': {
const options = { format: 'jwk' };
key = { ...options, key: keyPair.privateKey.export(options) };
fn = crypto.createPrivateKey;
break;
}
default:
throw new Error('not implemented');
}
measure(n, fn, key);
}