11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common.js');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
51cb0ef41Sopenharmony_ci  writes: [500],
61cb0ef41Sopenharmony_ci  cipher: ['AES192', 'AES256'],
71cb0ef41Sopenharmony_ci  type: ['asc', 'utf', 'buf'],
81cb0ef41Sopenharmony_ci  len: [2, 1024, 102400, 1024 * 1024],
91cb0ef41Sopenharmony_ci  api: ['legacy', 'stream'],
101cb0ef41Sopenharmony_ci}, {
111cb0ef41Sopenharmony_ci  flags: ['--no-warnings'],
121cb0ef41Sopenharmony_ci});
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cifunction main({ api, cipher, type, len, writes }) {
151cb0ef41Sopenharmony_ci  if (api === 'stream' && /^v0\.[0-8]\./.test(process.version)) {
161cb0ef41Sopenharmony_ci    console.error('Crypto streams not available until v0.10');
171cb0ef41Sopenharmony_ci    // Use the legacy, just so that we can compare them.
181cb0ef41Sopenharmony_ci    api = 'legacy';
191cb0ef41Sopenharmony_ci  }
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  const crypto = require('crypto');
221cb0ef41Sopenharmony_ci  const assert = require('assert');
231cb0ef41Sopenharmony_ci  const alice = crypto.getDiffieHellman('modp5');
241cb0ef41Sopenharmony_ci  const bob = crypto.getDiffieHellman('modp5');
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  alice.generateKeys();
271cb0ef41Sopenharmony_ci  bob.generateKeys();
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  const pubEnc = /^v0\.[0-8]/.test(process.version) ? 'binary' : null;
301cb0ef41Sopenharmony_ci  const alice_secret = alice.computeSecret(bob.getPublicKey(), pubEnc, 'hex');
311cb0ef41Sopenharmony_ci  const bob_secret = bob.computeSecret(alice.getPublicKey(), pubEnc, 'hex');
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  // alice_secret and bob_secret should be the same
341cb0ef41Sopenharmony_ci  assert(alice_secret === bob_secret);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  const alice_cipher = crypto.createCipher(cipher, alice_secret);
371cb0ef41Sopenharmony_ci  const bob_cipher = crypto.createDecipher(cipher, bob_secret);
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  let message;
401cb0ef41Sopenharmony_ci  let encoding;
411cb0ef41Sopenharmony_ci  switch (type) {
421cb0ef41Sopenharmony_ci    case 'asc':
431cb0ef41Sopenharmony_ci      message = 'a'.repeat(len);
441cb0ef41Sopenharmony_ci      encoding = 'ascii';
451cb0ef41Sopenharmony_ci      break;
461cb0ef41Sopenharmony_ci    case 'utf':
471cb0ef41Sopenharmony_ci      message = 'ü'.repeat(len / 2);
481cb0ef41Sopenharmony_ci      encoding = 'utf8';
491cb0ef41Sopenharmony_ci      break;
501cb0ef41Sopenharmony_ci    case 'buf':
511cb0ef41Sopenharmony_ci      message = Buffer.alloc(len, 'b');
521cb0ef41Sopenharmony_ci      break;
531cb0ef41Sopenharmony_ci    default:
541cb0ef41Sopenharmony_ci      throw new Error(`unknown message type: ${type}`);
551cb0ef41Sopenharmony_ci  }
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  const fn = api === 'stream' ? streamWrite : legacyWrite;
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  // Write data as fast as possible to alice, and have bob decrypt.
601cb0ef41Sopenharmony_ci  // use old API for comparison to v0.8
611cb0ef41Sopenharmony_ci  bench.start();
621cb0ef41Sopenharmony_ci  fn(alice_cipher, bob_cipher, message, encoding, writes);
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_cifunction streamWrite(alice, bob, message, encoding, writes) {
661cb0ef41Sopenharmony_ci  let written = 0;
671cb0ef41Sopenharmony_ci  bob.on('data', (c) => {
681cb0ef41Sopenharmony_ci    written += c.length;
691cb0ef41Sopenharmony_ci  });
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  bob.on('end', () => {
721cb0ef41Sopenharmony_ci    // Gbits
731cb0ef41Sopenharmony_ci    const bits = written * 8;
741cb0ef41Sopenharmony_ci    const gbits = bits / (1024 * 1024 * 1024);
751cb0ef41Sopenharmony_ci    bench.end(gbits);
761cb0ef41Sopenharmony_ci  });
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  alice.pipe(bob);
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  while (writes-- > 0)
811cb0ef41Sopenharmony_ci    alice.write(message, encoding);
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  alice.end();
841cb0ef41Sopenharmony_ci}
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_cifunction legacyWrite(alice, bob, message, encoding, writes) {
871cb0ef41Sopenharmony_ci  let written = 0;
881cb0ef41Sopenharmony_ci  let enc, dec;
891cb0ef41Sopenharmony_ci  for (let i = 0; i < writes; i++) {
901cb0ef41Sopenharmony_ci    enc = alice.update(message, encoding);
911cb0ef41Sopenharmony_ci    dec = bob.update(enc);
921cb0ef41Sopenharmony_ci    written += dec.length;
931cb0ef41Sopenharmony_ci  }
941cb0ef41Sopenharmony_ci  enc = alice.final();
951cb0ef41Sopenharmony_ci  dec = bob.update(enc);
961cb0ef41Sopenharmony_ci  written += dec.length;
971cb0ef41Sopenharmony_ci  dec = bob.final();
981cb0ef41Sopenharmony_ci  written += dec.length;
991cb0ef41Sopenharmony_ci  const gbits = written / (1024 * 1024 * 1024);
1001cb0ef41Sopenharmony_ci  bench.end(gbits);
1011cb0ef41Sopenharmony_ci}
102