11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common.js'); 41cb0ef41Sopenharmony_ciconst crypto = require('crypto'); 51cb0ef41Sopenharmony_ciconst fs = require('fs'); 61cb0ef41Sopenharmony_ciconst path = require('path'); 71cb0ef41Sopenharmony_ciconst fixtures_keydir = path.resolve(__dirname, '../../test/fixtures/keys/'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_cifunction readKey(name) { 101cb0ef41Sopenharmony_ci return fs.readFileSync(`${fixtures_keydir}/${name}.pem`, 'utf8'); 111cb0ef41Sopenharmony_ci} 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_cifunction readKeyPair(publicKeyName, privateKeyName) { 141cb0ef41Sopenharmony_ci return { 151cb0ef41Sopenharmony_ci publicKey: readKey(publicKeyName), 161cb0ef41Sopenharmony_ci privateKey: readKey(privateKeyName), 171cb0ef41Sopenharmony_ci }; 181cb0ef41Sopenharmony_ci} 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ciconst keyFixtures = { 211cb0ef41Sopenharmony_ci ec: readKeyPair('ec_p256_public', 'ec_p256_private'), 221cb0ef41Sopenharmony_ci rsa: readKeyPair('rsa_public_2048', 'rsa_private_2048'), 231cb0ef41Sopenharmony_ci ed25519: readKeyPair('ed25519_public', 'ed25519_private'), 241cb0ef41Sopenharmony_ci}; 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, { 271cb0ef41Sopenharmony_ci keyType: ['rsa', 'ec', 'ed25519'], 281cb0ef41Sopenharmony_ci keyFormat: ['pkcs8', 'spki', 'der-pkcs8', 'der-spki', 'jwk-public', 'jwk-private'], 291cb0ef41Sopenharmony_ci n: [1e3], 301cb0ef41Sopenharmony_ci}); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_cifunction measure(n, fn, input) { 331cb0ef41Sopenharmony_ci bench.start(); 341cb0ef41Sopenharmony_ci for (let i = 0; i < n; ++i) { 351cb0ef41Sopenharmony_ci fn(input); 361cb0ef41Sopenharmony_ci } 371cb0ef41Sopenharmony_ci bench.end(n); 381cb0ef41Sopenharmony_ci} 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_cifunction main({ n, keyFormat, keyType }) { 411cb0ef41Sopenharmony_ci const keyPair = { 421cb0ef41Sopenharmony_ci publicKey: crypto.createPublicKey(keyFixtures[keyType].publicKey), 431cb0ef41Sopenharmony_ci privateKey: crypto.createPrivateKey(keyFixtures[keyType].privateKey), 441cb0ef41Sopenharmony_ci }; 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci let key, fn; 471cb0ef41Sopenharmony_ci switch (keyFormat) { 481cb0ef41Sopenharmony_ci case 'spki': 491cb0ef41Sopenharmony_ci key = keyPair.publicKey.export({ format: 'pem', type: 'spki' }); 501cb0ef41Sopenharmony_ci fn = crypto.createPublicKey; 511cb0ef41Sopenharmony_ci break; 521cb0ef41Sopenharmony_ci case 'pkcs8': 531cb0ef41Sopenharmony_ci key = keyPair.privateKey.export({ format: 'pem', type: 'pkcs8' }); 541cb0ef41Sopenharmony_ci fn = crypto.createPrivateKey; 551cb0ef41Sopenharmony_ci break; 561cb0ef41Sopenharmony_ci case 'der-spki': { 571cb0ef41Sopenharmony_ci const options = { format: 'der', type: 'spki' }; 581cb0ef41Sopenharmony_ci key = { ...options, key: keyPair.publicKey.export(options) }; 591cb0ef41Sopenharmony_ci fn = crypto.createPublicKey; 601cb0ef41Sopenharmony_ci break; 611cb0ef41Sopenharmony_ci } 621cb0ef41Sopenharmony_ci case 'der-pkcs8': { 631cb0ef41Sopenharmony_ci const options = { format: 'der', type: 'pkcs8' }; 641cb0ef41Sopenharmony_ci key = { ...options, key: keyPair.privateKey.export(options) }; 651cb0ef41Sopenharmony_ci fn = crypto.createPrivateKey; 661cb0ef41Sopenharmony_ci break; 671cb0ef41Sopenharmony_ci } 681cb0ef41Sopenharmony_ci case 'jwk-public': { 691cb0ef41Sopenharmony_ci const options = { format: 'jwk' }; 701cb0ef41Sopenharmony_ci key = { ...options, key: keyPair.publicKey.export(options) }; 711cb0ef41Sopenharmony_ci fn = crypto.createPublicKey; 721cb0ef41Sopenharmony_ci break; 731cb0ef41Sopenharmony_ci } 741cb0ef41Sopenharmony_ci case 'jwk-private': { 751cb0ef41Sopenharmony_ci const options = { format: 'jwk' }; 761cb0ef41Sopenharmony_ci key = { ...options, key: keyPair.privateKey.export(options) }; 771cb0ef41Sopenharmony_ci fn = crypto.createPrivateKey; 781cb0ef41Sopenharmony_ci break; 791cb0ef41Sopenharmony_ci } 801cb0ef41Sopenharmony_ci default: 811cb0ef41Sopenharmony_ci throw new Error('not implemented'); 821cb0ef41Sopenharmony_ci } 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci measure(n, fn, key); 851cb0ef41Sopenharmony_ci} 86