constant_time_compare

Function constant_time_compare 

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

Performs constant-time comparison of two byte slices to prevent timing attacks.

This function compares two byte slices in constant time, meaning the execution time does not depend on where the first difference occurs. This is crucial for security-sensitive comparisons like tokens, passwords, or MAC verification.

§Arguments

  • a - First byte slice to compare
  • b - Second byte slice to compare

§Returns

  • true if the byte slices are equal, false otherwise

§Security Notes

This function is designed to prevent timing attacks by ensuring that the comparison time remains constant regardless of input values.

§Example

use auth_framework::secure_utils::constant_time_compare;

let token1 = b"secure_token_value";
let token2 = b"secure_token_value";
let token3 = b"different_token";

assert!(constant_time_compare(token1, token2));
assert!(!constant_time_compare(token1, token3));