forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargon2.js
More file actions
53 lines (47 loc) Β· 1.39 KB
/
argon2.js
File metadata and controls
53 lines (47 loc) Β· 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
const common = require('../common.js');
const { hasOpenSSL } = require('../../test/common/crypto.js');
const assert = require('node:assert');
const {
argon2,
argon2Sync,
randomBytes,
} = require('node:crypto');
if (!hasOpenSSL(3, 2)) {
console.log('Skipping: Argon2 requires OpenSSL >= 3.2');
process.exit(0);
}
const bench = common.createBenchmark(main, {
mode: ['sync', 'async'],
algorithm: ['argon2d', 'argon2i', 'argon2id'],
passes: [1, 3],
parallelism: [2, 4, 8],
memory: [2 ** 11, 2 ** 16, 2 ** 21],
n: [50],
});
function measureSync(n, algorithm, message, nonce, options) {
bench.start();
for (let i = 0; i < n; ++i)
argon2Sync(algorithm, { ...options, message, nonce, tagLength: 64 });
bench.end(n);
}
function measureAsync(n, algorithm, message, nonce, 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(algorithm, { ...options, message, nonce, tagLength: 64 }, done);
}
function main({ n, mode, algorithm, ...options }) {
// Message, nonce, secret, associated data & tag length do not affect performance
const message = randomBytes(32);
const nonce = randomBytes(16);
if (mode === 'sync')
measureSync(n, algorithm, message, nonce, options);
else
measureAsync(n, algorithm, message, nonce, options);
}