Changelog - 2026-11-06
Hash.equals compares two digests in constant time, and Hash.verify says its text is exact
New Feature SecurityIn one line. Hash.equals({ value, expected }) compares two strings you already hold, such as an idempotency fingerprint against a saved one, without the timing leak of ===.
What changed
- A new static method.
Hash.equals({ value, expected })checks the byte length first and then compares withcrypto.timingSafeEqual. Any difference answersfalse, and so does a value that is not a string; it never throws. Two empty strings are equal. Hash.verifyuses it. Nothing changes for callers:verifystill hashesvalue(or keys it withsecret) and then compares.expectedis exact text. The comparison is of the text, not of the bytes it stands for. Upper-case hex is not lower-case hex, and an unpadded or URL-safe form of the same base64 digest is refused. Pass the matchingoutputEncoding(base64url), or normalise the text first.
typescript
const same = Hash.equals({ value: fingerprint, expected: saved.fingerprint });
const ok = Hash.withAlgorithm('sha256').verify({
value: rawBody,
expected: signature,
opts: { outputEncoding: 'base64url' },
});Who is affected
- Apps that compare two stored digests with
===. Switch toHash.equals. - Everyone else. No action needed.
Details
| File | Package |
|---|---|
src/modules/crypto/algorithms/hash.algorithm.ts | helpers |