DynamicRandom

Struct DynamicRandom 

Source
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, ExposeSecret};
let random = DynamicRandom::generate(64);
assert_eq!(random.len(), 64);

Implementations§

Source§

impl DynamicRandom

Source

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);
Source

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());
Source

pub fn into_inner(self) -> Dynamic<Vec<u8>>

Consume and return the inner Dynamic<Vec<u8>>.

Source§

impl DynamicRandom

Source

pub fn to_hex(&self) -> HexString

Borrowing encode (original random remains usable).

Source

pub fn into_hex(self) -> HexString

Consuming encode (raw random bytes zeroized immediately).

Source§

impl DynamicRandom

Source

pub fn to_base64url(&self) -> Base64String

Borrowing encode (original random remains usable).

Source

pub fn into_base64url(self) -> Base64String

Consuming encode (raw random bytes zeroized immediately).

Source§

impl DynamicRandom

Source

pub fn try_to_bech32( &self, hrp: &str, ) -> Result<Bech32String, Bech32EncodingError>

Borrowing encode (original random remains usable).

Source

pub fn try_to_bech32m( &self, hrp: &str, ) -> Result<Bech32String, Bech32EncodingError>

Source

pub fn try_into_bech32( self, hrp: &str, ) -> Result<Bech32String, Bech32EncodingError>

Consuming encode (raw random bytes zeroized immediately).

Source

pub fn try_into_bech32m( self, hrp: &str, ) -> Result<Bech32String, Bech32EncodingError>

Trait Implementations§

Source§

impl Debug for DynamicRandom

Debug implementation (always redacted).

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl ExposeSecret for DynamicRandom

Available on crate feature rand only.

Implementation for DynamicRandom - read-only access.

Random wrappers only provide read-only access to prevent invalidation of the randomly generated secret. The Inner type is [u8] (slice) for compatibility with SecureRandom trait bounds.

Source§

type Inner = [u8]

The inner secret type being exposed. Read more
Source§

fn expose_secret(&self) -> &[u8]

Expose the secret for read-only access.
Source§

fn len(&self) -> usize

Returns the length of the secret.
Source§

fn is_empty(&self) -> bool

Returns true if the secret is empty.
Source§

impl From<DynamicRandom> for Dynamic<Vec<u8>>

Source§

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();
Source§

impl SecureRandom for DynamicRandom

Available on crate feature rand only.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V