extrapaytr-crypto 0.1.0

Digest, HMAC, constant-time comparison and at-rest sealing helpers for ExtraPayTR
Documentation
use subtle::ConstantTimeEq;

/// Constant-time byte comparison. Always use this (never `==`) when
/// comparing a computed signature/digest against one supplied by a
/// provider or caller, to avoid timing side-channels.
pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
    if a.len() != b.len() {
        return false;
    }
    a.ct_eq(b).into()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn equal_slices_match() {
        assert!(constant_time_eq(b"abc123", b"abc123"));
    }

    #[test]
    fn different_slices_do_not_match() {
        assert!(!constant_time_eq(b"abc123", b"abc124"));
    }

    #[test]
    fn different_lengths_do_not_match() {
        assert!(!constant_time_eq(b"abc", b"abcd"));
    }
}