hash
ModuleThe password hash trait, and the constant-time comparison and secure randomness the hashes and proofs share.
Contents
use utopia_auth::Hash;pub trait Hash: Debug + Send + SyncA password hashing algorithm, with its options.
The built-in algorithms live in crate::hashes; implement this trait
to register another one with Password::add_hash.
Hashing is CPU bound: async callers run it on the blocking pool.
use utopia_auth::hash_equals;pub fn hash_equals(known: &[u8], user: &[u8]) -> boolCompares two byte strings in constant time, to check secrets without leaking how much of them matched.
Strings of different lengths are unequal at once.
use utopia_auth::random_bytes;pub fn random_bytes(buf: &mut [u8])Fills buf with cryptographically secure random bytes.
The bytes come from the thread's CSPRNG (rand::rng): ChaCha12
keyed with 256 bits from the operating system's random source, and
rekeyed from it after every 64 KiB of output. ChaCha with 12 rounds
has no known attack better than brute force on its 256-bit key, so its
output is indistinguishable from the OS source's to any feasible
adversary; rekeying limits a leaked state to 64 KiB of output. The
OS source is itself a generator of this kind, seeded by the kernel;
reading it once per 64 KiB instead of once per call removes
a system call from every token, the same trade OpenBSD and macOS make
in arc4random_buf(). The generator is per thread, never shared, and
never copied; a forked child must not keep using its parent's thread
(servers here never fork after start).
Panics when the system random source is unavailable.