Compare secrets in constant time

Javascript

A plain === leaks how much of the token matched through its timing. This does not.

const { timingSafeEqual } = require('node:crypto');

function safeEqual(a, b) {
  const ba = Buffer.from(a);
  const bb = Buffer.from(b);
  if (ba.length !== bb.length) return false;
  return timingSafeEqual(ba, bb);
}

More in Security

Random picks