Skip to main content

rws_base/core/
mod.rs

1//! Small shared utilities: [`base64`], [`sha256`] (SHA-256, HMAC-SHA256) and the [`New`] trait (a constructor
2//! used across the crate, much like `Default`).
3
4pub mod base64;
5pub mod json;
6pub mod sha256;
7pub mod x509;
8
9/// Compares in time independent of where the inputs first differ, so response
10/// timing doesn't reveal how much of a guessed token was right.
11#[doc(hidden)]
12pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
13    if a.len() != b.len() {
14        return false;
15    }
16    a.iter().zip(b).fold(0u8, |diff, (x, y)| diff | (x ^ y)) == 0
17}
18
19pub trait New {
20    fn new() -> Self;
21}