pub struct Fixed<T>(/* private fields */);Expand description
Re-export of the Fixed type.
Stack-allocated secure secret wrapper.
This is a zero-cost wrapper for fixed-size secrets like byte arrays or primitives. The inner field is private, forcing all access through explicit methods.
Security invariants:
- No
DereforAsRef— prevents silent access or borrowing. - No implicit
Copy— even for[u8; N], duplication must be explicit via.clone(). Debugis always redacted.
§Examples
Basic usage:
use secure_gate::{Fixed, ExposeSecret};
let secret = Fixed::new(42u32);
assert_eq!(*secret.expose_secret(), 42);For byte arrays (most common):
use secure_gate::{fixed_alias, Fixed, ExposeSecret};
fixed_alias!(Aes256Key, 32);
let key_bytes = [0x42u8; 32];
let key: Aes256Key = Fixed::from(key_bytes);
assert_eq!(key.len(), 32);
assert_eq!(key.expose_secret()[0], 0x42);With zeroize feature (automatic wipe on drop):
use secure_gate::Fixed;
let mut secret = Fixed::new([1u8, 2, 3]);
drop(secret); // memory wiped automaticallyImplementations§
impl<const N: usize> Fixed<[u8; N]>
§Byte-array specific helpers
Source§impl<const N: usize> Fixed<[u8; N]>
Constant-time equality — only available with ct-eq feature.
impl<const N: usize> Fixed<[u8; N]>
Constant-time equality — only available with ct-eq feature.
Sourcepub fn ct_eq(&self, other: &Self) -> bool
pub fn ct_eq(&self, other: &Self) -> bool
Constant-time equality comparison.
This is the only safe way to compare two fixed-size secrets.
Available only when the ct-eq feature is enabled.
§Example
use secure_gate::Fixed;
let a = Fixed::new([1u8; 32]);
let b = Fixed::new([1u8; 32]);
assert!(a.ct_eq(&b));Source§impl<const N: usize> Fixed<[u8; N]>
Random generation — only available with rand feature.
impl<const N: usize> Fixed<[u8; N]>
Random generation — only available with rand feature.
Sourcepub fn generate_random() -> Self
pub fn generate_random() -> Self
Generate fresh random bytes using the OS RNG.
This is a convenience method that generates random bytes directly
without going through FixedRandom. Equivalent to:
FixedRandom::<N>::generate().into_inner()
§Example
use secure_gate::Fixed;
let key: Fixed<[u8; 32]> = Fixed::generate_random();Sourcepub fn try_generate_random() -> Result<Self, OsError>
pub fn try_generate_random() -> Result<Self, OsError>
Try to generate random bytes for Fixed.
Returns an error if the RNG fails.
§Example
use secure_gate::Fixed;
let key: Result<Fixed<[u8; 32]>, rand::rand_core::OsError> = Fixed::try_generate_random();
assert!(key.is_ok());Source§impl<const N: usize> Fixed<CloneableArrayInner<N>>
impl<const N: usize> Fixed<CloneableArrayInner<N>>
Sourcepub fn init_with<F>(constructor: F) -> Self
pub fn init_with<F>(constructor: F) -> Self
Construct a cloneable array secret by building it in a closure.
Same stack-minimization benefits as CloneableString::init_with.
§Example
use secure_gate::CloneableArray;
let key = CloneableArray::<32>::init_with(|| {
let mut arr = [0u8; 32];
// Fill from some source...
arr
});Sourcepub fn try_init_with<F, E>(constructor: F) -> Result<Self, E>
pub fn try_init_with<F, E>(constructor: F) -> Result<Self, E>
Fallible version of init_with.
Same stack-minimization benefits as init_with, but allows for construction
that may fail with an error. Useful when reading secrets from fallible sources
like files or network connections.
Trait Implementations§
Source§impl<T: CloneSafe> Clone for Fixed<T>
Available on crate feature zeroize only.Opt-in Clone — only for types marked CloneSafe (default no-clone).
impl<T: CloneSafe> Clone for Fixed<T>
zeroize only.Opt-in Clone — only for types marked CloneSafe (default no-clone).
Source§impl<const N: usize, T> ExposeSecret for Fixed<[T; N]>
Implementation for [Fixed<[T; N]>] - provides full read/write access for arrays.
impl<const N: usize, T> ExposeSecret for Fixed<[T; N]>
Implementation for [Fixed<[T; N]>] - provides full read/write access for arrays.
Fixed is a core wrapper that allows both reading and mutation of secrets.
This implementation directly accesses the inner field.
Source§impl<const N: usize> ExposeSecret for Fixed<CloneableArrayInner<N>>
Available on crate feature zeroize only.Implementation for Fixed<CloneableArrayInner<N>> - exposes the inner wrapper.
impl<const N: usize> ExposeSecret for Fixed<CloneableArrayInner<N>>
zeroize only.Implementation for Fixed<CloneableArrayInner<N>> - exposes the inner wrapper.
Source§type Inner = CloneableArrayInner<N>
type Inner = CloneableArrayInner<N>
Source§fn expose_secret(&self) -> &CloneableArrayInner<N>
fn expose_secret(&self) -> &CloneableArrayInner<N>
Source§impl ExposeSecret for Fixed<u32>
Implementation for Fixed<u32> - provides access for test compatibility.
impl ExposeSecret for Fixed<u32>
Implementation for Fixed<u32> - provides access for test compatibility.
Source§impl<const N: usize, T> ExposeSecretMut for Fixed<[T; N]>
Implementation for [Fixed<[T; N]>] - provides mutable access for arrays.
impl<const N: usize, T> ExposeSecretMut for Fixed<[T; N]>
Implementation for [Fixed<[T; N]>] - provides mutable access for arrays.
Extends the read-only implementation with mutation capabilities.
Source§fn expose_secret_mut(&mut self) -> &mut [T; N]
fn expose_secret_mut(&mut self) -> &mut [T; N]
Source§impl<const N: usize> ExposeSecretMut for Fixed<CloneableArrayInner<N>>
Available on crate feature zeroize only.Implementation for Fixed<CloneableArrayInner<N>> - provides mutable access.
impl<const N: usize> ExposeSecretMut for Fixed<CloneableArrayInner<N>>
zeroize only.Implementation for Fixed<CloneableArrayInner<N>> - provides mutable access.
Source§fn expose_secret_mut(&mut self) -> &mut CloneableArrayInner<N>
fn expose_secret_mut(&mut self) -> &mut CloneableArrayInner<N>
Source§impl ExposeSecretMut for Fixed<u32>
Implementation for Fixed<u32> - provides mutable access.
impl ExposeSecretMut for Fixed<u32>
Implementation for Fixed<u32> - provides mutable access.
Source§fn expose_secret_mut(&mut self) -> &mut u32
fn expose_secret_mut(&mut self) -> &mut u32
Source§impl<const N: usize> From<FixedRandom<N>> for Fixed<[u8; N]>
impl<const N: usize> From<FixedRandom<N>> for Fixed<[u8; N]>
Source§fn from(rng: FixedRandom<N>) -> Self
fn from(rng: FixedRandom<N>) -> Self
Convert a FixedRandom to Fixed, transferring ownership.
This preserves all security guarantees. The FixedRandom type
ensures the value came from secure RNG, and this conversion
transfers that value to Fixed without exposing bytes.
§Example
use secure_gate::{Fixed, random::FixedRandom};
let key: Fixed<[u8; 32]> = FixedRandom::<32>::generate().into();Source§impl<const N: usize> TryFrom<&[u8]> for Fixed<[u8; N]>
Implements TryFrom<&[u8]> for creating a Fixed from a byte slice of exact length.
impl<const N: usize> TryFrom<&[u8]> for Fixed<[u8; N]>
Implements TryFrom<&[u8]> for creating a Fixed from a byte slice of exact length.
Source§impl<T: Zeroize> Zeroize for Fixed<T>
Available on crate feature zeroize only.Zeroize integration.
impl<T: Zeroize> Zeroize for Fixed<T>
zeroize only.Zeroize integration.
impl<T: Zeroize> ZeroizeOnDrop for Fixed<T>
zeroize only.Zeroize on drop integration.