constantTimeEquals static method
Constant-time comparison of two byte arrays.
- Runs in time proportional to the longest input.
- Does not early-return on first difference.
HINT: Use to compare HMAC tags and other secret values.
Implementation
static bool constantTimeEquals(Uint8List a, Uint8List b) {
var diff = a.length ^ b.length; // include length diff
final maxLen = a.length > b.length ? a.length : b.length;
for (var i = 0; i < maxLen; i++) {
final ai = i < a.length ? a[i] : 0;
final bi = i < b.length ? b[i] : 0;
diff |= (ai ^ bi);
}
return diff == 0;
}