2016-02-19 17:03:16 -08:00
|
|
|
'use strict';
|
2019-01-21 01:22:27 +01:00
|
|
|
// Throughput benchmark in signing and verifying
|
2017-09-13 22:48:53 -03:00
|
|
|
const common = require('../common.js');
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const fixtures_keydir = path.resolve(__dirname, '../../test/fixtures/keys/');
|
2022-09-03 14:46:47 +00:00
|
|
|
const keylen_list = ['2048', '4096'];
|
2017-09-13 22:48:53 -03:00
|
|
|
const RSA_PublicPem = {};
|
|
|
|
const RSA_PrivatePem = {};
|
2015-04-02 13:30:35 +09:00
|
|
|
|
2019-02-04 22:06:08 -08:00
|
|
|
keylen_list.forEach((key) => {
|
2017-04-17 04:01:12 +03:00
|
|
|
RSA_PublicPem[key] =
|
|
|
|
fs.readFileSync(`${fixtures_keydir}/rsa_public_${key}.pem`);
|
|
|
|
RSA_PrivatePem[key] =
|
|
|
|
fs.readFileSync(`${fixtures_keydir}/rsa_private_${key}.pem`);
|
2015-04-02 13:30:35 +09:00
|
|
|
});
|
|
|
|
|
2017-09-13 22:48:53 -03:00
|
|
|
const bench = common.createBenchmark(main, {
|
2015-04-02 13:30:35 +09:00
|
|
|
n: [500],
|
|
|
|
keylen: keylen_list,
|
2023-02-10 01:54:53 +01:00
|
|
|
len: [16, 32, 64],
|
2015-04-02 13:30:35 +09:00
|
|
|
});
|
|
|
|
|
2018-01-23 13:17:56 +01:00
|
|
|
function main({ len, algo, keylen, n }) {
|
|
|
|
const message = Buffer.alloc(len, 'b');
|
2015-04-02 13:30:35 +09:00
|
|
|
bench.start();
|
2018-01-23 13:17:56 +01:00
|
|
|
StreamWrite(algo, keylen, message, n, len);
|
2015-04-02 13:30:35 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
function StreamWrite(algo, keylen, message, n, len) {
|
2017-09-13 22:48:53 -03:00
|
|
|
const written = n * len;
|
|
|
|
const bits = written * 8;
|
|
|
|
const kbits = bits / (1024);
|
2015-04-02 13:30:35 +09:00
|
|
|
|
2017-09-13 22:48:53 -03:00
|
|
|
const privateKey = RSA_PrivatePem[keylen];
|
|
|
|
const publicKey = RSA_PublicPem[keylen];
|
2019-12-30 12:20:07 +01:00
|
|
|
for (let i = 0; i < n; i++) {
|
2017-09-13 22:48:53 -03:00
|
|
|
const enc = crypto.privateEncrypt(privateKey, message);
|
2016-02-24 22:30:10 -08:00
|
|
|
crypto.publicDecrypt(publicKey, enc);
|
2015-04-02 13:30:35 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
bench.end(kbits);
|
|
|
|
}
|