pub struct DynamicRandom(/* private fields */);Expand description
Heap-allocated cryptographically secure random bytes with encoding methods.
This is a newtype over Dynamic<Vec<u8>> for semantic clarity.
Like FixedRandom, guarantees freshness via RNG construction.
Requires the rand feature.
Supports direct encoding to Hex, Base64, Bech32, and Bech32m via convenience methods.
§Examples
use secure_gate::random::DynamicRandom;
let random = DynamicRandom::generate(64);
assert_eq!(random.len(), 64);Implementations§
Source§impl DynamicRandom
impl DynamicRandom
Sourcepub fn generate(len: usize) -> Self
pub fn generate(len: usize) -> Self
Generate fresh random bytes of the specified length.
Panics if the RNG fails.
§Example
use secure_gate::random::DynamicRandom;
let random = DynamicRandom::generate(128);Sourcepub fn try_generate(len: usize) -> Result<Self, OsError>
pub fn try_generate(len: usize) -> Result<Self, OsError>
Try to generate fresh random bytes of the specified length.
Returns an error if the RNG fails.
§Example
use secure_gate::random::DynamicRandom;
let random: Result<DynamicRandom, rand::rand_core::OsError> = DynamicRandom::try_generate(64);
assert!(random.is_ok());Sourcepub fn expose_secret(&self) -> &[u8] ⓘ
pub fn expose_secret(&self) -> &[u8] ⓘ
Expose the random bytes for read-only access.
Sourcepub fn into_inner(self) -> Dynamic<Vec<u8>>
pub fn into_inner(self) -> Dynamic<Vec<u8>>
Consume and return the inner Dynamic<Vec<u8>>.
Source§impl DynamicRandom
impl DynamicRandom
Sourcepub fn into_base64(self) -> Base64String
pub fn into_base64(self) -> Base64String
Consume self and return the random bytes as a validated base64 string.
The raw bytes are zeroized immediately after encoding.
Source§impl DynamicRandom
impl DynamicRandom
Sourcepub fn to_base64(&self) -> Base64String
pub fn to_base64(&self) -> Base64String
Borrow and encode the random bytes as a validated base64 string (allocates).
The original secret remains intact and usable.
Source§impl DynamicRandom
impl DynamicRandom
Sourcepub fn try_into_bech32(
self,
hrp: &str,
) -> Result<Bech32String, Bech32EncodingError>
pub fn try_into_bech32( self, hrp: &str, ) -> Result<Bech32String, Bech32EncodingError>
Consume self and return the random bytes as a validated Bech32 string with the specified HRP.
The raw bytes are zeroized immediately after encoding (via drop of self).
§Errors
Returns an error if the HRP is invalid.
Sourcepub fn try_into_bech32m(
self,
hrp: &str,
) -> Result<Bech32String, Bech32EncodingError>
pub fn try_into_bech32m( self, hrp: &str, ) -> Result<Bech32String, Bech32EncodingError>
Consume self and return the random bytes as a validated Bech32m string with the specified HRP.
The raw bytes are zeroized immediately after encoding (via drop of self).
§Errors
Returns an error if the HRP is invalid.
Source§impl DynamicRandom
impl DynamicRandom
Sourcepub fn try_to_bech32(
&self,
hrp: &str,
) -> Result<Bech32String, Bech32EncodingError>
pub fn try_to_bech32( &self, hrp: &str, ) -> Result<Bech32String, Bech32EncodingError>
Borrow and encode the random bytes as a validated Bech32 string with the specified HRP (allocates).
The original secret remains intact and usable.
§Errors
Returns an error if the HRP is invalid.
Sourcepub fn try_to_bech32m(
&self,
hrp: &str,
) -> Result<Bech32String, Bech32EncodingError>
pub fn try_to_bech32m( &self, hrp: &str, ) -> Result<Bech32String, Bech32EncodingError>
Borrow and encode the random bytes as a validated Bech32m string with the specified HRP (allocates).
The original secret remains intact and usable.
§Errors
Returns an error if the HRP is invalid.
Source§impl DynamicRandom
impl DynamicRandom
Trait Implementations§
Source§impl Debug for DynamicRandom
Debug implementation (always redacted).
impl Debug for DynamicRandom
Debug implementation (always redacted).
Source§impl From<DynamicRandom> for Dynamic<Vec<u8>>
impl From<DynamicRandom> for Dynamic<Vec<u8>>
Source§fn from(rng: DynamicRandom) -> Self
fn from(rng: DynamicRandom) -> Self
Convert a DynamicRandom to Dynamic, transferring ownership.
This preserves all security guarantees. The DynamicRandom 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::DynamicRandom};
let random: Dynamic<Vec<u8>> = DynamicRandom::generate(64).into();