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'];
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  writes: [500],
211cb0ef41Sopenharmony_ci  algo: ['SHA1', 'SHA224', 'SHA256', 'SHA384', 'SHA512'],
221cb0ef41Sopenharmony_ci  keylen: keylen_list,
231cb0ef41Sopenharmony_ci  len: [1024, 102400, 2 * 102400, 3 * 102400, 1024 * 1024],
241cb0ef41Sopenharmony_ci});
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_cifunction main({ len, algo, keylen, writes }) {
271cb0ef41Sopenharmony_ci  const message = Buffer.alloc(len, 'b');
281cb0ef41Sopenharmony_ci  bench.start();
291cb0ef41Sopenharmony_ci  StreamWrite(algo, keylen, message, writes, len);
301cb0ef41Sopenharmony_ci}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cifunction StreamWrite(algo, keylen, message, writes, len) {
331cb0ef41Sopenharmony_ci  const written = writes * len;
341cb0ef41Sopenharmony_ci  const bits = written * 8;
351cb0ef41Sopenharmony_ci  const kbits = bits / (1024);
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  const privateKey = RSA_PrivatePem[keylen];
381cb0ef41Sopenharmony_ci  const s = crypto.createSign(algo);
391cb0ef41Sopenharmony_ci  const v = crypto.createVerify(algo);
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  while (writes-- > 0) {
421cb0ef41Sopenharmony_ci    s.update(message);
431cb0ef41Sopenharmony_ci    v.update(message);
441cb0ef41Sopenharmony_ci  }
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  s.sign(privateKey, 'binary');
471cb0ef41Sopenharmony_ci  s.end();
481cb0ef41Sopenharmony_ci  v.end();
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  bench.end(kbits);
511cb0ef41Sopenharmony_ci}
52