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']; 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 writes: [500], 21 algo: ['SHA1', 'SHA224', 'SHA256', 'SHA384', 'SHA512'], 22 keylen: keylen_list, 23 len: [1024, 102400, 2 * 102400, 3 * 102400, 1024 * 1024], 24}); 25 26function main({ len, algo, keylen, writes }) { 27 const message = Buffer.alloc(len, 'b'); 28 bench.start(); 29 StreamWrite(algo, keylen, message, writes, len); 30} 31 32function StreamWrite(algo, keylen, message, writes, len) { 33 const written = writes * len; 34 const bits = written * 8; 35 const kbits = bits / (1024); 36 37 const privateKey = RSA_PrivatePem[keylen]; 38 const s = crypto.createSign(algo); 39 const v = crypto.createVerify(algo); 40 41 while (writes-- > 0) { 42 s.update(message); 43 v.update(message); 44 } 45 46 s.sign(privateKey, 'binary'); 47 s.end(); 48 v.end(); 49 50 bench.end(kbits); 51} 52