pub struct Dynamic<T: ?Sized>(/* private fields */);Expand description
Heap-allocated secure secret wrapper.
This is a thin wrapper around Box<T> with enforced explicit exposure.
Suitable for dynamic-sized secrets like String or Vec<u8>.
Security invariants:
- No
DereforAsRef— prevents silent access. Debugis always redacted.- With
zeroize, wipes the entire allocation on drop (including spare capacity).
§Examples
Basic usage:
use secure_gate::Dynamic;
let secret: Dynamic<String> = "hunter2".into();
assert_eq!(secret.expose_secret(), "hunter2");Mutable access:
use secure_gate::Dynamic;
let mut secret = Dynamic::<String>::new("pass".to_string());
secret.expose_secret_mut().push('!');
assert_eq!(secret.expose_secret(), "pass!");With zeroize (automatic wipe):
use secure_gate::Dynamic;
let secret = Dynamic::<Vec<u8>>::new(vec![1u8; 32]);
drop(secret); // heap wiped automaticallyImplementations§
Source§impl<T: ?Sized> Dynamic<T>
impl<T: ?Sized> Dynamic<T>
Sourcepub fn new_boxed(value: Box<T>) -> Self
pub fn new_boxed(value: Box<T>) -> Self
Wrap an already-boxed value.
Zero-cost — just wraps the Box.
Sourcepub const fn expose_secret(&self) -> &T
pub const fn expose_secret(&self) -> &T
Expose the inner value for read-only access.
This is the only way to read the secret — loud and auditable.
Sourcepub fn expose_secret_mut(&mut self) -> &mut T
pub fn expose_secret_mut(&mut self) -> &mut T
Expose the inner value for mutable access.
This is the only way to mutate the secret — loud and auditable.
Source§impl Dynamic<Vec<u8>>
impl Dynamic<Vec<u8>>
Sourcepub fn generate_random(len: usize) -> Self
pub fn generate_random(len: usize) -> Self
Generate fresh random bytes of the specified length using the OS RNG.
This is a convenience method that generates random bytes directly
without going through DynamicRng. Equivalent to:
DynamicRng::generate(len).into_inner()
§Example
use secure_gate::Dynamic;
let random: Dynamic<Vec<u8>> = Dynamic::generate_random(64);
assert_eq!(random.len(), 64);Sourcepub fn try_generate_random(len: usize) -> Result<Self, OsError>
pub fn try_generate_random(len: usize) -> Result<Self, OsError>
Try to generate random bytes for Dynamic.
Returns an error if the RNG fails.
§Example
use secure_gate::Dynamic;
let random: Result<Dynamic<Vec<u8>>, rand::rand_core::OsError> = Dynamic::try_generate_random(64);
assert!(random.is_ok());Source§impl Dynamic<CloneableStringInner>
impl Dynamic<CloneableStringInner>
pub const fn expose_inner(&self) -> &String
pub fn expose_inner_mut(&mut self) -> &mut String
Sourcepub fn init_with<F>(constructor: F) -> Self
pub fn init_with<F>(constructor: F) -> Self
Construct a cloneable string secret by building it in a closure.
This minimizes the time the secret spends on the stack:
- The closure builds a temporary
String. - It is immediately cloned to the heap.
- The temporary is zeroized before returning.
Use this when reading passwords or tokens from user input.
§Example
use secure_gate::CloneableString;
use std::io::{self, Write};
fn read_password() -> io::Result<String> {
let mut input = String::new();
io::stdout().flush()?;
io::stdin().read_line(&mut input)?;
Ok(input.trim_end().to_string())
}
let pw = CloneableString::init_with(|| read_password().unwrap());Source§impl Dynamic<CloneableVecInner>
impl Dynamic<CloneableVecInner>
pub const fn expose_inner(&self) -> &Vec<u8> ⓘ
pub fn expose_inner_mut(&mut self) -> &mut Vec<u8> ⓘ
Sourcepub fn init_with<F>(constructor: F) -> Self
pub fn init_with<F>(constructor: F) -> Self
Construct a cloneable vec secret by building it in a closure.
Same stack-minimization benefits as CloneableString::init_with.
§Example
use secure_gate::CloneableVec;
let seed = CloneableVec::init_with(|| {
let mut v = vec![0u8; 32];
// Fill from some source...
v
});Trait Implementations§
Source§impl<T: CloneableSecretMarker> Clone for Dynamic<T>
Available on crate feature zeroize only.
impl<T: CloneableSecretMarker> Clone for Dynamic<T>
zeroize only.Source§impl From<DynamicRng> for Dynamic<Vec<u8>>
impl From<DynamicRng> for Dynamic<Vec<u8>>
Source§fn from(rng: DynamicRng) -> Self
fn from(rng: DynamicRng) -> Self
Convert a DynamicRng to Dynamic, transferring ownership.
This preserves all security guarantees. The DynamicRng type
ensures the value came from secure RNG, and this conversion
transfers that value to Dynamic without exposing bytes.
§Example
use secure_gate::{Dynamic, random::DynamicRng};
let random: Dynamic<Vec<u8>> = DynamicRng::generate(64).into();impl<T: ?Sized + Zeroize> ZeroizeOnDrop for Dynamic<T>
zeroize only.