Core - v1.0.0
    Preparing search index...

    Function timingSafeEqual

    • Compares two strings without leaking, through how long the comparison took, how much of the expected value the caller already got right.

      Parameters

      • a: string

        First value to compare.

      • b: string

        Second value to compare.

      Returns boolean

      True only when both values are byte-for-byte identical.

      JavaScript's === on strings returns at the first differing byte. Over many requests that difference is measurable, and it turns guessing a secret from an exponential search into a linear one: an attacker who can time responses recovers the value one byte at a time. This walks the whole buffer regardless and accumulates the difference with a bitwise OR, so the work done is the same for a value that matches on no bytes as for one that matches on all but the last.

      Length is the exception and cannot be hidden — a differing length exits immediately. That leaks only the size of the secret, which is not the part worth protecting.

      Use this for any value a caller could otherwise guess a byte at a time: API keys, webhook secrets, signatures. Comparing a hash of a value is not a substitute, because the hashes are compared the same way.

      if (!timingSafeEqual(provided, expected)) {
      return ctx.json({ message: 'Unauthorized' }, 401)
      }

      Bayu Dwiyan Satria

      1.0.0

      1.0.0