crypto.timingSafeEqual(a, b)


此函数使用常数时间算法比较表示给定 ArrayBufferTypedArrayDataView 实例的底层字节。

【This function compares the underlying bytes that represent the given ArrayBuffer, TypedArray, or DataView instances using a constant-time algorithm.】

此函数不会泄露允许攻击者猜测某个值的时间信息。它适用于比较 HMAC 摘要或类似认证 Cookie 或能力 URL的秘密值。

【This function does not leak timing information that would allow an attacker to guess one of the values. This is suitable for comparing HMAC digests or secret values like authentication cookies or capability urls.】

ab 都必须是 BufferTypedArrayDataView,并且它们必须具有相同的字节长度。如果 ab 的字节长度不同,将抛出错误。

如果 ab 中至少有一个是每个条目超过一个字节的 TypedArray,例如 Uint16Array,则结果将使用平台字节顺序计算。

【If at least one of a and b is a TypedArray with more than one byte per entry, such as Uint16Array, the result will be computed using the platform byte order.】

当两个输入都是 Float32ArrayFloat64Array 时,由于浮点数的 IEEE 754 编码,这个函数可能会返回意想不到的结果。特别是,x === yObject.is(x, y) 并不意味着两个浮点数 xy 的字节表示相同。

使用 crypto.timingSafeEqual 并不能保证 周围 的代码是时间安全的。应谨慎确保周围的代码不会引入时间漏洞。

【Use of crypto.timingSafeEqual does not guarantee that the surrounding code is timing-safe. Care should be taken to ensure that the surrounding code does not introduce timing vulnerabilities.】