crypto.timingSafeEqual(a, b)


该函数基于恒定时间算法。 如果 a 等于 b,则返回 true,而不会泄露允许攻击者猜测其中一个值的时间信息。 这适用于比较 HMAC 摘要或秘密值,如身份验证 cookie 或功能网址

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

This function is based on a constant-time algorithm. Returns true if a is equal to b, without leaking 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.

a and b must both be Buffers, TypedArrays, or DataViews, and they must have the same length.

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.