From f2e66e2774b091c690657006d3d24cb7172a9312 Mon Sep 17 00:00:00 2001 From: Ranieri Althoff Date: Sun, 29 Oct 2023 18:44:14 +0100 Subject: [PATCH] Add benchmark for Argon2 --- benchmark/crypto/argon2.js | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 benchmark/crypto/argon2.js diff --git a/benchmark/crypto/argon2.js b/benchmark/crypto/argon2.js new file mode 100644 index 00000000000000..c3751d1332322b --- /dev/null +++ b/benchmark/crypto/argon2.js @@ -0,0 +1,51 @@ +'use strict'; + +const common = require('../common.js'); +const assert = require('node:assert'); +const { + argon2, + argon2Sync, +} = require('node:crypto'); + +const bench = common.createBenchmark(main, { + sync: [0, 1], + size: [32, 64, 1024], + pass: ['a', 'secret', 'this-is-a-much-longer-password'], + saltlen: [8, 16, 32], + algorithm: ['ARGON2D', 'ARGON2I', 'ARGON2ID'], + secretlen: [0, 8], + adlen: [0, 12], + iter: [2, 3, 4], + memcost: [64 << 10, 256 << 10], + n: [50], +}); + +function measureSync(n, size, pass, salt, options) { + bench.start(); + for (let i = 0; i < n; ++i) + argon2Sync(pass, salt, size, options); + bench.end(n); +} + +function measureAsync(n, size, pass, salt, options) { + let remaining = n; + function done(err) { + assert.ifError(err); + if (--remaining === 0) + bench.end(n); + } + bench.start(); + for (let i = 0; i < n; ++i) + argon2(pass, salt, size, options, done); +} + +function main({ n, sync, size, pass, saltlen, secretlen, adlen, ...options }) { + const salt = Buffer.alloc(saltlen, 0x01); + const secret = Buffer.alloc(secretlen, 0x02); + const ad = Buffer.alloc(adlen, 0x03); + options = { ...options, secret, ad }; + if (sync) + measureSync(n, size, pass, salt, options); + else + measureAsync(n, size, pass, salt, options); +}