Skip to main content

timing_safe_equal

Function timing_safe_equal 

Source
pub fn timing_safe_equal(a: &[u8], b: &[u8]) -> bool
Expand description

Perform a constant-time comparison of two byte slices.

This function takes the same amount of time regardless of where the first difference occurs, preventing timing attacks.

§Security Note

Always use this function when comparing proofs, tokens, or any security-sensitive values. Regular == comparison can leak information about where differences occur.

§Example

use ash_core::timing_safe_equal;

let a = b"secret_proof_123";
let b = b"secret_proof_123";
let c = b"secret_proof_456";

assert!(timing_safe_equal(a, b));
assert!(!timing_safe_equal(a, c));