equals static method

bool equals(
  1. String a,
  2. String b
)

true when a and b are the same. It takes the same time for any two values of the same length, so the time does not tell how much of a secret (an API key, a token) was right.

if (!Hash.equals(req.header('X-Api-Key') ?? '', apiKey)) { ... }

Implementation

static bool equals(String a, String b) {
  List<int> x = utf8.encode(a);
  List<int> y = utf8.encode(b);
  if (x.length != y.length) return false;
  int diff = 0;
  for (int i = 0; i < x.length; i++) {
    diff |= x[i] ^ y[i];
  }
  return diff == 0;
}