pub struct ConstantTimeOps;Expand description
Constant-time comparison utilities for security tokens Uses subtle crate to ensure comparisons take the same time regardless of where differences occur
Implementations§
Source§impl ConstantTimeOps
impl ConstantTimeOps
Sourcepub fn compare(expected: &[u8], actual: &[u8]) -> bool
pub fn compare(expected: &[u8], actual: &[u8]) -> bool
Compare two byte slices in constant time
Returns true if equal, false otherwise. Time is independent of where the difference occurs, preventing timing attacks.
§Arguments
expected- The expected (correct/known) valueactual- The actual (untrusted) value from the user/attacker
§Examples
let stored_token = b"secret_token_value";
let user_token = b"user_provided_token";
assert!(!ConstantTimeOps::compare(stored_token, user_token));Sourcepub fn compare_str(expected: &str, actual: &str) -> bool
pub fn compare_str(expected: &str, actual: &str) -> bool
Compare two strings in constant time
Converts strings to bytes and performs constant-time comparison. Useful for comparing JWT tokens, session tokens, or other string-based secrets.
§Arguments
expected- The expected (correct/known) string valueactual- The actual (untrusted) string value from the user/attacker
Sourcepub fn compare_len_safe(expected: &[u8], actual: &[u8]) -> bool
pub fn compare_len_safe(expected: &[u8], actual: &[u8]) -> bool
Compare two slices with different lengths in constant time
If lengths differ, still compares as much as possible to avoid leaking length information through timing.
Sourcepub fn compare_jwt(expected: &str, actual: &str) -> bool
pub fn compare_jwt(expected: &str, actual: &str) -> bool
Compare JWT tokens in constant time Handles the common case of JWT with header.payload.signature format
Sourcepub fn compare_session_token(expected: &str, actual: &str) -> bool
pub fn compare_session_token(expected: &str, actual: &str) -> bool
Compare session tokens in constant time Handles session_id:signature format
Sourcepub fn compare_csrf_token(expected: &str, actual: &str) -> bool
pub fn compare_csrf_token(expected: &str, actual: &str) -> bool
Compare CSRF tokens in constant time
Sourcepub fn compare_hmac(expected: &[u8], actual: &[u8]) -> bool
pub fn compare_hmac(expected: &[u8], actual: &[u8]) -> bool
Compare HMAC signatures in constant time Used for verifying webhook signatures and other HMAC-based authenticity
Sourcepub fn compare_refresh_token(expected: &str, actual: &str) -> bool
pub fn compare_refresh_token(expected: &str, actual: &str) -> bool
Compare refresh tokens in constant time
Sourcepub fn compare_auth_code(expected: &str, actual: &str) -> bool
pub fn compare_auth_code(expected: &str, actual: &str) -> bool
Compare authorization codes in constant time (used in OAuth flows)
Sourcepub fn compare_pkce_verifier(expected: &str, actual: &str) -> bool
pub fn compare_pkce_verifier(expected: &str, actual: &str) -> bool
Compare PKCE code verifier in constant time
Sourcepub fn compare_state_token(expected: &str, actual: &str) -> bool
pub fn compare_state_token(expected: &str, actual: &str) -> bool
Compare state tokens in constant time (CSRF protection in OAuth)
Auto Trait Implementations§
impl Freeze for ConstantTimeOps
impl RefUnwindSafe for ConstantTimeOps
impl Send for ConstantTimeOps
impl Sync for ConstantTimeOps
impl Unpin for ConstantTimeOps
impl UnwindSafe for ConstantTimeOps
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request