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));Sourcepub fn to_hex(&self) -> String
pub fn to_hex(&self) -> String
Encodes the secret bytes as a lowercase hex string.
Delegates to ToHex::to_hex on the inner [u8; N].
Requires the encoding-hex feature.
Sourcepub fn to_hex_upper(&self) -> String
pub fn to_hex_upper(&self) -> String
Encodes the secret bytes as an uppercase hex string.
Delegates to ToHex::to_hex_upper on the inner [u8; N].
Requires the encoding-hex feature.
Sourcepub fn to_base64url(&self) -> String
pub fn to_base64url(&self) -> String
Encodes the secret bytes as an unpadded Base64url string.
Delegates to ToBase64Url::to_base64url on the inner [u8; N].
Requires the encoding-base64 feature.
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 (SysRng). Requires the rand feature.
Heap-free and works in no_std / no_alloc builds.
§Panics
Panics if the system RNG fails to provide bytes (TryRng::try_fill_bytes
returns Err). This is treated as a fatal environment error.
§Examples
use secure_gate::{Fixed, RevealSecret};
let key: Fixed<[u8; 32]> = Fixed::from_random();
assert_eq!(key.len(), 32);Sourcepub fn from_rng<R: TryRng + TryCryptoRng>(rng: &mut R) -> Result<Self, R::Error>
pub fn from_rng<R: TryRng + TryCryptoRng>(rng: &mut R) -> Result<Self, R::Error>
Fills a new [u8; N] from rng and wraps it.
Accepts any TryCryptoRng + TryRng — for example,
a seeded StdRng for deterministic tests. Requires the rand
feature. Heap-free.
§Errors
Returns R::Error if try_fill_bytes fails.
§Examples
use rand::rngs::StdRng;
use rand::SeedableRng;
use secure_gate::Fixed;
let mut rng = StdRng::from_seed([1u8; 32]);
let key: Fixed<[u8; 16]> = Fixed::from_rng(&mut rng).expect("rng fill");Source§impl<const N: usize> Fixed<[u8; N]>
impl<const N: usize> Fixed<[u8; N]>
Sourcepub fn try_from_hex(hex: &str) -> Result<Self, HexError>
pub fn try_from_hex(hex: &str) -> Result<Self, HexError>
Decodes a lowercase hex string into Fixed<[u8; N]>.
The decoded bytes are held in a Zeroizing<Vec<u8>> until copied onto
the stack array, so the temporary heap buffer is zeroed even if a panic
occurs mid-flight.
§Errors
Returns HexError::InvalidLength if the decoded length does not equal N,
or a parse error if the input is not valid hex.
§Note
Unlike Dynamic::try_from_hex, the secret
lives on the stack inside a [u8; N]. Stack residue behaviour after the
Fixed is dropped and zeroized is discussed in SECURITY.md.
Source§impl<const N: usize> Fixed<[u8; N]>
impl<const N: usize> Fixed<[u8; N]>
Sourcepub fn try_from_base64url(s: &str) -> Result<Self, Base64Error>
pub fn try_from_base64url(s: &str) -> Result<Self, Base64Error>
Decodes an unpadded Base64url string into Fixed<[u8; N]>.
The decoded bytes are held in a Zeroizing<Vec<u8>> until copied onto
the stack array, so the temporary heap buffer is zeroed even if a panic
occurs mid-flight.
§Errors
Returns Base64Error::InvalidLength if the decoded length does not equal N,
or a parse error if the input is not valid Base64url.
§Note
Unlike Dynamic::try_from_base64url, the
secret lives on the stack inside a [u8; N]. Stack residue behaviour after the
Fixed is dropped and zeroized is discussed in SECURITY.md.
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.