Blockchain Commons Components TypeScript Library - v1.0.0-alpha.13
    Preparing search index...

    Function bytesEqual

    • Compare two Uint8Arrays for equality using constant-time comparison.

      This function is designed to be resistant to timing attacks by always comparing all bytes regardless of where a difference is found. The comparison time depends only on the length of the arrays, not on where they differ.

      Security Note: If the arrays have different lengths, this function returns false immediately, which does leak length information. For cryptographic uses where length should also be secret, ensure both arrays are the same length before comparison.

      Parameters

      • a: Uint8Array

        First byte array

      • b: Uint8Array

        Second byte array

      Returns boolean

      true if both arrays have the same length and identical contents

      const key1 = new Uint8Array([1, 2, 3, 4]);
      const key2 = new Uint8Array([1, 2, 3, 4]);
      const key3 = new Uint8Array([1, 2, 3, 5]);

      bytesEqual(key1, key2); // true
      bytesEqual(key1, key3); // false