mirror of
https://github.com/nodejs/node.git
synced 2025-05-07 15:35:41 +00:00

PR-URL: https://github.com/nodejs/node/pull/29657 Co-authored-by: ZaneHannanAU <ZaneHannanAU@users.noreply.github.com> Co-authored-by: Rich Trott <rtrott@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
65 lines
1.6 KiB
JavaScript
65 lines
1.6 KiB
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
if (!common.hasCrypto)
|
|
common.skip('missing crypto');
|
|
|
|
const assert = require('assert');
|
|
const crypto = require('crypto');
|
|
|
|
// 'should consider equal strings to be equal'
|
|
assert.strictEqual(
|
|
crypto.timingSafeEqual(Buffer.from('foo'), Buffer.from('foo')),
|
|
true
|
|
);
|
|
|
|
// 'should consider unequal strings to be unequal'
|
|
assert.strictEqual(
|
|
crypto.timingSafeEqual(Buffer.from('foo'), Buffer.from('bar')),
|
|
false
|
|
);
|
|
|
|
{
|
|
// Test TypedArrays with different lengths but equal byteLengths.
|
|
const buf = crypto.randomBytes(16).buffer;
|
|
const a1 = new Uint8Array(buf);
|
|
const a2 = new Uint16Array(buf);
|
|
const a3 = new Uint32Array(buf);
|
|
|
|
for (const left of [a1, a2, a3]) {
|
|
for (const right of [a1, a2, a3]) {
|
|
assert.strictEqual(crypto.timingSafeEqual(left, right), true);
|
|
}
|
|
}
|
|
}
|
|
|
|
common.expectsError(
|
|
() => crypto.timingSafeEqual(Buffer.from([1, 2, 3]), Buffer.from([1, 2])),
|
|
{
|
|
code: 'ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH',
|
|
type: RangeError,
|
|
message: 'Input buffers must have the same byte length'
|
|
}
|
|
);
|
|
|
|
common.expectsError(
|
|
() => crypto.timingSafeEqual('not a buffer', Buffer.from([1, 2])),
|
|
{
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
type: TypeError,
|
|
message:
|
|
'The "buf1" argument must be one of type Buffer, TypedArray, or ' +
|
|
'DataView. Received type string'
|
|
}
|
|
);
|
|
|
|
common.expectsError(
|
|
() => crypto.timingSafeEqual(Buffer.from([1, 2]), 'not a buffer'),
|
|
{
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
type: TypeError,
|
|
message:
|
|
'The "buf2" argument must be one of type Buffer, TypedArray, or ' +
|
|
'DataView. Received type string'
|
|
}
|
|
);
|