1'use strict'; 2// Throughput benchmark in signing and verifying 3const common = require('../common.js'); 4const crypto = require('crypto'); 5const fs = require('fs'); 6const path = require('path'); 7const fixtures_keydir = path.resolve(__dirname, '../../test/fixtures/keys/'); 8const keylen_list = ['2048', '4096']; 9const RSA_PublicPem = {}; 10const RSA_PrivatePem = {}; 11 12keylen_list.forEach((key) => { 13 RSA_PublicPem[key] = 14 fs.readFileSync(`${fixtures_keydir}/rsa_public_${key}.pem`); 15 RSA_PrivatePem[key] = 16 fs.readFileSync(`${fixtures_keydir}/rsa_private_${key}.pem`); 17}); 18 19const bench = common.createBenchmark(main, { 20 n: [500], 21 keylen: keylen_list, 22 len: [16, 32, 64], 23}); 24 25function main({ len, algo, keylen, n }) { 26 const message = Buffer.alloc(len, 'b'); 27 bench.start(); 28 StreamWrite(algo, keylen, message, n, len); 29} 30 31function StreamWrite(algo, keylen, message, n, len) { 32 const written = n * len; 33 const bits = written * 8; 34 const kbits = bits / (1024); 35 36 const privateKey = RSA_PrivatePem[keylen]; 37 const publicKey = RSA_PublicPem[keylen]; 38 for (let i = 0; i < n; i++) { 39 const enc = crypto.privateEncrypt(privateKey, message); 40 crypto.publicDecrypt(publicKey, enc); 41 } 42 43 bench.end(kbits); 44} 45