node/benchmark/crypto/timingSafeEqual.js
Tobias Nießen 08609b5222
crypto: make timingSafeEqual faster for Uint8Array
Add a fast API that V8 can use if the user supplies Uint8Arrays
(including Buffers) to timingSafeEqual.

PR-URL: https://github.com/nodejs/node/pull/52341
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2024-04-08 11:36:53 +00:00

23 lines
582 B
JavaScript

'use strict';
const common = require('../common.js');
const assert = require('node:assert');
const { randomBytes, timingSafeEqual } = require('node:crypto');
const bench = common.createBenchmark(main, {
n: [1e5],
bufferSize: [10, 100, 200, 2_100, 22_023],
});
function main({ n, bufferSize }) {
const bufs = [randomBytes(bufferSize), randomBytes(bufferSize)];
bench.start();
let count = 0;
for (let i = 0; i < n; i++) {
const ret = timingSafeEqual(bufs[i % 2], bufs[1]);
if (ret) count++;
}
bench.end(n);
assert.strictEqual(count, Math.floor(n / 2));
}