pub fn constant_time_eq(a: &[u8], b: &[u8]) -> boolExpand description
Compare two byte slices without returning early on a difference.
The loop always runs max(a.len(), b.len()) iterations and ORs every
difference into one accumulator, so the time spent does not depend on where
the inputs differ — a plain == on String/&[u8] stops at the first
mismatching byte and leaks how long a shared prefix is.
Length is not hidden. The runtime is proportional to the longer input and the result distinguishes lengths, which is safe because the length of a secret is public by construction (framework secrets are at least 32 bytes and digests have a fixed size); only the content must stay secret. Do not use this to compare values whose length is itself confidential — pad both sides first.
assert!(constant_time_eq(b"correct horse", b"correct horse"));
assert!(!constant_time_eq(b"correct horse", b"correct horsX"));
assert!(!constant_time_eq(b"short", b"short and longer"));