#[must_use]
#[inline]
pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
if a.len() != b.len() {
return false;
}
use subtle::ConstantTimeEq;
a.ct_eq(b).into()
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, missing_docs)]
mod tests {
use super::*;
#[test]
fn kernel_matches_lib_reexport() {
assert_eq!(
constant_time_eq(b"abc", b"abc"),
crate::crypto::constant_time_eq(b"abc", b"abc")
);
assert_eq!(
constant_time_eq(b"abc", b"abd"),
crate::crypto::constant_time_eq(b"abc", b"abd")
);
assert_eq!(
constant_time_eq(b"abc", b"ab"),
crate::crypto::constant_time_eq(b"abc", b"ab")
);
}
#[test]
fn empty_slices_equal() {
assert!(constant_time_eq(b"", b""));
}
#[test]
fn length_mismatch_rejected() {
assert!(!constant_time_eq(b"aaaa", b"aaa"));
}
}