Skip to main content

Token

Struct Token 

Source
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>

Source

pub fn random() -> Result<Self>

Source

pub fn from_bytes(data: [u8; LEN]) -> Self

Source

pub fn to_base64(&self) -> String

Source

pub fn as_bytes(&self) -> &[u8]

Source

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.

Source

pub fn encrypt( &self, key: &[u8], algo: CipherSuite, ) -> Result<EncryptedToken<LEN>>

Source

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.

Source

pub fn to_blake3(&self, pepper: &[u8]) -> TokenHash

Hashes the token using BLAKE3 with an external pepper.

§Warning

This is NOT a password hashing function. BLAKE3 is intentionally fast and must only be used with high-entropy, randomly generated secrets.

Trait Implementations§

Source§

impl<const LEN: usize> Clone for Token<LEN>

Source§

fn clone(&self) -> Token<LEN>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const LEN: usize> Debug for Token<LEN>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const LEN: usize> Display for Token<LEN>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const LEN: usize> Drop for Token<LEN>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more