pub struct Token<const LEN: usize>(/* private fields */);Expand description
Cryptographically-random, fixed-size token material.
§Security notes
- Intended for high-entropy secrets such as session IDs, API tokens, CSRF tokens, and refresh tokens.
- NOT suitable for passwords or other low-entropy user-provided secrets.
- Should never be stored directly; store only a cryptographic hash (e.g. BLAKE3 with a server-side pepper).
Equality comparison should be performed on hashes, not tokens. Original token material cannot be recovered.
Implementations§
Source§impl<const LEN: usize> Token<LEN>
impl<const LEN: usize> Token<LEN>
pub fn random() -> Result<Self>
pub fn from_bytes(data: [u8; LEN]) -> Self
pub fn to_base64(&self) -> String
pub fn as_bytes(&self) -> &[u8] ⓘ
Sourcepub fn from_base64(data: &str) -> Result<Self>
pub fn from_base64(data: &str) -> Result<Self>
Decodes a URL-safe, unpadded base64 token.
Fails if the decoded byte length does not exactly match LEN,
preventing truncated or partially-zeroed secrets.
pub fn encrypt( &self, key: &[u8], algo: CipherSuite, ) -> Result<EncryptedToken<LEN>>
Sourcepub fn to_default_hash(&self, pepper: &[u8]) -> TokenHash
pub fn to_default_hash(&self, pepper: &[u8]) -> TokenHash
Hashes the token using the currently selected default hashing algorithm.
This method exists as a stable indirection point:
- New tokens should always be hashed using this function.
- The underlying algorithm may change over time (e.g. when introducing a new hash version), without requiring call-site changes.
- Existing stored hashes remain verifiable via
TokenHash.
The pepper must be a secret value stored separately from the database, typically in configuration or a secret manager. Compromise of both database and pepper allows token forgery.
The default algorithm MUST remain suitable only for high-entropy, randomly generated tokens.
Trait Implementations§
Auto Trait Implementations§
impl<const LEN: usize> Freeze for Token<LEN>
impl<const LEN: usize> RefUnwindSafe for Token<LEN>
impl<const LEN: usize> Send for Token<LEN>
impl<const LEN: usize> Sync for Token<LEN>
impl<const LEN: usize> Unpin for Token<LEN>
impl<const LEN: usize> UnsafeUnpin for Token<LEN>
impl<const LEN: usize> UnwindSafe for Token<LEN>
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
Converts
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>
Converts
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 more