pub struct Fixed<T: Zeroize> { /* private fields */ }Expand description
Stack-allocated secret wrapper with explicit access and automatic zeroization on drop.
Always available. Inner type must implement Zeroize.
Zero-cost stack-allocated wrapper for fixed-size secrets.
Always available. Inner type must implement Zeroize for automatic zeroization on drop.
No Deref, AsRef, or Copy by default — all access requires
expose_secret() or
with_secret() (scoped, preferred).
For construction of Fixed<[u8; N]>, new_with is the
matching scoped constructor — it writes directly into the wrapper’s storage
and avoids any intermediate stack copy. new(value) remains
available as the ergonomic default.
Debug always prints [REDACTED]. Performance indistinguishable from raw arrays.
Implementations§
Source§impl<const N: usize> Fixed<[u8; N]>
Construction and ergonomic encoding helpers for Fixed<[u8; N]>.
impl<const N: usize> Fixed<[u8; N]>
Construction and ergonomic encoding helpers for Fixed<[u8; N]>.
Sourcepub fn new_with<F>(f: F) -> Self
pub fn new_with<F>(f: F) -> Self
Writes directly into the wrapper’s storage via a user-supplied closure,
eliminating the intermediate stack copy that new may produce.
The array is zero-initialized before the closure runs. Prefer this over
new(value) when minimizing stack residue matters
(long-lived keys, high-assurance environments).
§Examples
use secure_gate::Fixed;
let secret = Fixed::<[u8; 4]>::new_with(|arr| arr.fill(0xAB));pub fn to_hex(&self) -> String
pub fn to_hex_upper(&self) -> String
pub fn to_base64url(&self) -> String
Source§impl<const N: usize> Fixed<[u8; N]>
impl<const N: usize> Fixed<[u8; N]>
Sourcepub fn from_random() -> Self
pub fn from_random() -> Self
Fills a new [u8; N] with cryptographically secure random bytes and wraps it.
Uses the system RNG (OsRng). Requires the rand feature.
Heap-free and works in no_std / no_alloc builds.
§Panics
Panics if the RNG fails (TryRngCore::try_fill_bytes
returns Err). This is treated as a fatal environment error.
Sourcepub fn from_rng<R: TryRngCore + TryCryptoRng>(
rng: &mut R,
) -> Result<Self, R::Error>
pub fn from_rng<R: TryRngCore + TryCryptoRng>( rng: &mut R, ) -> Result<Self, R::Error>
Fills a new [u8; N] from rng and wraps it.
Accepts any TryCryptoRng + TryRngCore (e.g. a
seeded generator for deterministic tests). Requires the rand feature. Heap-free.
§Errors
Returns R::Error if try_fill_bytes fails.
Source§impl<const N: usize> Fixed<[u8; N]>
impl<const N: usize> Fixed<[u8; N]>
pub fn try_from_hex(hex: &str) -> Result<Self, HexError>
Source§impl<const N: usize> Fixed<[u8; N]>
impl<const N: usize> Fixed<[u8; N]>
pub fn try_from_base64url(s: &str) -> Result<Self, Base64Error>
Source§impl<const N: usize> Fixed<[u8; N]>
impl<const N: usize> Fixed<[u8; N]>
Sourcepub fn try_from_bech32_unchecked(s: &str) -> Result<Self, Bech32Error>
pub fn try_from_bech32_unchecked(s: &str) -> Result<Self, Bech32Error>
Decodes a Bech32 (BIP-173) string into Fixed<[u8; N]>.
§Warning
The HRP is not validated — any HRP will be accepted as long as the checksum
is valid and the payload length equals N. For security-critical code where
cross-protocol confusion must be prevented, use try_from_bech32.
Sourcepub fn try_from_bech32(s: &str, expected_hrp: &str) -> Result<Self, Bech32Error>
pub fn try_from_bech32(s: &str, expected_hrp: &str) -> Result<Self, Bech32Error>
Decodes a Bech32 (BIP-173) string into Fixed<[u8; N]>, validating that the HRP
matches expected_hrp (case-insensitive).
Prefer this over try_from_bech32_unchecked in
security-critical code to prevent cross-protocol confusion attacks.
Source§impl<const N: usize> Fixed<[u8; N]>
impl<const N: usize> Fixed<[u8; N]>
Sourcepub fn try_from_bech32m_unchecked(s: &str) -> Result<Self, Bech32Error>
pub fn try_from_bech32m_unchecked(s: &str) -> Result<Self, Bech32Error>
Decodes a Bech32m (BIP-350) string into Fixed<[u8; N]>.
§Warning
The HRP is not validated — any HRP will be accepted as long as the checksum
is valid and the payload length equals N. For security-critical code where
cross-protocol confusion must be prevented, use try_from_bech32m.
Sourcepub fn try_from_bech32m(
s: &str,
expected_hrp: &str,
) -> Result<Self, Bech32Error>
pub fn try_from_bech32m( s: &str, expected_hrp: &str, ) -> Result<Self, Bech32Error>
Decodes a Bech32m (BIP-350) string into Fixed<[u8; N]>, validating that the HRP
matches expected_hrp (case-insensitive).
Prefer this over try_from_bech32m_unchecked in
security-critical code to prevent cross-protocol confusion attacks.
Trait Implementations§
Source§impl<T: Zeroize + CloneableSecret> Clone for Fixed<T>
Available on crate feature cloneable only.
impl<T: Zeroize + CloneableSecret> Clone for Fixed<T>
cloneable only.Source§impl<T> ConstantTimeEq for Fixed<T>where
T: ConstantTimeEq + Zeroize,
Available on crate feature ct-eq only.
impl<T> ConstantTimeEq for Fixed<T>where
T: ConstantTimeEq + Zeroize,
ct-eq only.Source§impl<'de, const N: usize> Deserialize<'de> for Fixed<[u8; N]>
Available on crate feature serde-deserialize only.
impl<'de, const N: usize> Deserialize<'de> for Fixed<[u8; N]>
serde-deserialize only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<const N: usize, T: Zeroize> ExposeSecret<[T; N]> for Fixed<[T; N]>
impl<const N: usize, T: Zeroize> ExposeSecret<[T; N]> for Fixed<[T; N]>
Source§fn expose_secret(&self) -> &[T; N]
fn expose_secret(&self) -> &[T; N]
Source§impl<const N: usize, T: Zeroize> ExposeSecretMut<[T; N]> for Fixed<[T; N]>
impl<const N: usize, T: Zeroize> ExposeSecretMut<[T; N]> for Fixed<[T; N]>
Source§fn expose_secret_mut(&mut self) -> &mut [T; N]
fn expose_secret_mut(&mut self) -> &mut [T; N]
Source§impl<T: Clone + Zeroize, const N: usize> From<Fixed<[T; N]>> for Secret<[T; N]>
Converts a Fixed<[T; N]> into Secret<[T; N]>.
impl<T: Clone + Zeroize, const N: usize> From<Fixed<[T; N]>> for Secret<[T; N]>
Converts a Fixed<[T; N]> into Secret<[T; N]>.
Source§impl<T: Clone + Zeroize, const N: usize> From<Secret<[T; N]>> for Fixed<[T; N]>
Converts Secret<[T; N]> into a Fixed<[T; N]>.
impl<T: Clone + Zeroize, const N: usize> From<Secret<[T; N]>> for Fixed<[T; N]>
Converts Secret<[T; N]> into a Fixed<[T; N]>.
Source§impl<const N: usize, T: Zeroize> RevealSecret for Fixed<[T; N]>
Explicit access to immutable [Fixed<[T; N]>] contents.
impl<const N: usize, T: Zeroize> RevealSecret for Fixed<[T; N]>
Explicit access to immutable [Fixed<[T; N]>] contents.
Source§fn with_secret<F, R>(&self, f: F) -> R
fn with_secret<F, R>(&self, f: F) -> R
Source§fn expose_secret(&self) -> &[T; N]
fn expose_secret(&self) -> &[T; N]
Source§impl<const N: usize, T: Zeroize> RevealSecretMut for Fixed<[T; N]>
Explicit access to mutable [Fixed<[T; N]>] contents.
impl<const N: usize, T: Zeroize> RevealSecretMut for Fixed<[T; N]>
Explicit access to mutable [Fixed<[T; N]>] contents.
Source§impl<T: Zeroize + SerializableSecret> Serialize for Fixed<T>
Available on crate feature serde-serialize only.
impl<T: Zeroize + SerializableSecret> Serialize for Fixed<T>
serde-serialize only.