Crypto

crypto.timingSafeEqual()

Compares equal-length byte sequences using a timing-safe primitive. Reject length mismatches before calling. Surrounding code can still leak timing information; this is not complete authentication. Tested on Node.js 22.16.0. Example output (one line per entry): ["true false false"].

Formal Syntax & Structure

nodejs syntaxnodejs
crypto.timingSafeEqual(a, b)

Parameters & Attributes

NameTypeDescription
aBuffer | TypedArray | DataView | ArrayBufferFirst byte sequence.
bBuffer | TypedArray | DataView | ArrayBufferSecond sequence of equal byte length.

Return Value

boolean

Equality result.

Working Example

Example Snippetnodejs
import { timingSafeEqual } from 'node:crypto';const match = (a, b) => a.length === b.length && timingSafeEqual(a, b);console.log(match(Buffer.from('abc'), Buffer.from('abc')), match(Buffer.from('abc'), Buffer.from('abd')), match(Buffer.from('abc'), Buffer.from('ab')));
Node.js Reference