11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Throughput benchmark in signing and verifying
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_ciconst keylen_list = ['2048', '4096'];
91cb0ef41Sopenharmony_ciconst RSA_PublicPem = {};
101cb0ef41Sopenharmony_ciconst RSA_PrivatePem = {};
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cikeylen_list.forEach((key) => {
131cb0ef41Sopenharmony_ci  RSA_PublicPem[key] =
141cb0ef41Sopenharmony_ci    fs.readFileSync(`${fixtures_keydir}/rsa_public_${key}.pem`);
151cb0ef41Sopenharmony_ci  RSA_PrivatePem[key] =
161cb0ef41Sopenharmony_ci    fs.readFileSync(`${fixtures_keydir}/rsa_private_${key}.pem`);
171cb0ef41Sopenharmony_ci});
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
201cb0ef41Sopenharmony_ci  n: [500],
211cb0ef41Sopenharmony_ci  keylen: keylen_list,
221cb0ef41Sopenharmony_ci  len: [16, 32, 64],
231cb0ef41Sopenharmony_ci});
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cifunction main({ len, algo, keylen, n }) {
261cb0ef41Sopenharmony_ci  const message = Buffer.alloc(len, 'b');
271cb0ef41Sopenharmony_ci  bench.start();
281cb0ef41Sopenharmony_ci  StreamWrite(algo, keylen, message, n, len);
291cb0ef41Sopenharmony_ci}
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_cifunction StreamWrite(algo, keylen, message, n, len) {
321cb0ef41Sopenharmony_ci  const written = n * len;
331cb0ef41Sopenharmony_ci  const bits = written * 8;
341cb0ef41Sopenharmony_ci  const kbits = bits / (1024);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  const privateKey = RSA_PrivatePem[keylen];
371cb0ef41Sopenharmony_ci  const publicKey = RSA_PublicPem[keylen];
381cb0ef41Sopenharmony_ci  for (let i = 0; i < n; i++) {
391cb0ef41Sopenharmony_ci    const enc = crypto.privateEncrypt(privateKey, message);
401cb0ef41Sopenharmony_ci    crypto.publicDecrypt(publicKey, enc);
411cb0ef41Sopenharmony_ci  }
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  bench.end(kbits);
441cb0ef41Sopenharmony_ci}
45