Cloudflare 中文文档
Workers
Workers
编辑这个页面
跳转官方原文档
Set theme to dark (⇧+D)

Using timingSafeEqual

Protect against timing attacks by safely comparing values using timingSafeEqual.

The crypto.subtle.timingSafeEqual function compares two values using a constant-time algorithm. The time taken is independent of the contents of the values.

When strings are compared using the equality operator (== or ===), the comparison will end at the first mismatched character. By using timingSafeEqual, an attacker would not be able to use timing to find where at which point in the two strings there is a difference.

The timingSafeEqual function takes two ArrayBuffer or TypedArray values to compare. These buffers must be of equal length, otherwise an exception is thrown.

In order to compare two strings, you must use the TextEncoder API. Since the time taken to encode the values may reveal the length of our secret value, you should check the length of the strings before encoding.