pub struct TokenSecret(/* private fields */);Expand description
Zeroizing wrapper for HMAC token secrets.
§Security Properties
- Memory is zeroed on drop via
zeroize(throughsecrecy::SecretBox) - Explicitly does NOT implement:
Debug,Display,Clone,PartialEq,Serialize,Deserialize– preventing accidental logging, serialization, or copying - Secret bytes accessed only via
expose_secret()which returns&[u8]
§Threat Model
Protects against: accidental logging, memory dumps after drop, clone-and-forget patterns, comparison side channels, JSON serialization leakage. Does NOT protect against: active memory forensics while the secret is in use, side-channel attacks on the HMAC computation itself.
§Usage in Structs
When embedding TokenSecret in a struct that derives Serialize:
#[derive(serde::Serialize)]
struct MyServer {
#[serde(skip)] // REQUIRED -- TokenSecret does not implement Serialize
token_secret: TokenSecret,
// ... other fields
}Implementations§
Source§impl TokenSecret
impl TokenSecret
Sourcepub fn new(secret: impl Into<Vec<u8>>) -> Self
pub fn new(secret: impl Into<Vec<u8>>) -> Self
Create from raw bytes. The input Vec is consumed and its contents
copied into a SecretBox. The original Vec is NOT zeroed – callers
should use from_env() for maximum security.
Sourcepub fn from_env(var: &str) -> Result<Self, VarError>
pub fn from_env(var: &str) -> Result<Self, VarError>
Read from an environment variable. The string value is converted to bytes and wrapped immediately.
Sourcepub fn expose_secret(&self) -> &[u8] ⓘ
pub fn expose_secret(&self) -> &[u8] ⓘ
Expose the secret bytes for cryptographic operations. Callers MUST NOT log or persist the returned slice.
Auto Trait Implementations§
impl Freeze for TokenSecret
impl RefUnwindSafe for TokenSecret
impl Send for TokenSecret
impl Sync for TokenSecret
impl Unpin for TokenSecret
impl UnsafeUnpin for TokenSecret
impl UnwindSafe for TokenSecret
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 more