FixedRandom

Struct FixedRandom 

Source
pub struct FixedRandom<const N: usize>(/* private fields */);
Expand description

Fixed-length cryptographically secure random value with encoding methods.

This is a newtype over Fixed<[u8; N]> that enforces construction only via secure RNG. Guarantees freshness — cannot be created from arbitrary bytes.

Requires the rand feature.

Supports direct encoding to Hex, Base64, Bech32, and Bech32m via convenience methods.

§Examples

Basic usage:

use secure_gate::random::FixedRandom;
let random: FixedRandom<32> = FixedRandom::generate();
assert_eq!(random.len(), 32);

With alias:

use secure_gate::fixed_alias_random;
fixed_alias_random!(Nonce, 24);
let nonce = Nonce::generate();

Implementations§

Source§

impl<const N: usize> FixedRandom<N>

Source

pub fn generate() -> Self

Generate fresh random bytes using the OS RNG.

Uses rand::rngs::OsRng directly for maximum throughput. Panics if the RNG fails (rare, but correct for crypto code).

§Example
use secure_gate::random::FixedRandom;
let random = FixedRandom::<16>::generate();
assert!(!random.is_empty());
Source

pub fn try_generate() -> Result<Self, OsError>

Try to generate fresh random bytes using the OS RNG.

Returns an error if the RNG fails.

§Example
use secure_gate::random::FixedRandom;
let random: Result<FixedRandom<32>, rand::rand_core::OsError> = FixedRandom::try_generate();
assert!(random.is_ok());
Source

pub fn expose_secret(&self) -> &[u8; N]

Expose the random bytes for read-only access.

§Example
use secure_gate::random::FixedRandom;
let random = FixedRandom::<4>::generate();
let bytes = random.expose_secret();
Source

pub const fn len(&self) -> usize

Returns the fixed length in bytes.

Source

pub const fn is_empty(&self) -> bool

Returns true if the length is zero.

Source

pub fn into_inner(self) -> Fixed<[u8; N]>

Consume the wrapper and return the inner Fixed<[u8; N]>.

This transfers ownership without exposing the secret bytes. The returned Fixed retains all security guarantees (zeroize, etc.).

§Example
use secure_gate::{Fixed, random::FixedRandom};
let random = FixedRandom::<32>::generate();
let fixed: Fixed<[u8; 32]> = random.into_inner();
// Can now use fixed.expose_secret() as needed
Source§

impl<const N: usize> FixedRandom<N>

Source

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<const N: usize> FixedRandom<N>

Source

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<const N: usize> FixedRandom<N>

Source

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.

Source

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<const N: usize> FixedRandom<N>

Source

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.

Source

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<const N: usize> FixedRandom<N>

Source

pub fn into_hex(self) -> HexString

Consume self and return the random bytes as a validated hex string.

The raw bytes are zeroized immediately after encoding.

Source§

impl<const N: usize> FixedRandom<N>

Source

pub fn to_hex(&self) -> HexString

Borrow and encode the random bytes as a validated lowercase hex string (allocates).

The original secret remains intact and usable.

Trait Implementations§

Source§

impl<const N: usize> Debug for FixedRandom<N>

Debug implementation (always redacted).

Source§

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

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

impl<const N: usize> From<FixedRandom<N>> for Fixed<[u8; N]>

Source§

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

Auto Trait Implementations§

§

impl<const N: usize> Freeze for FixedRandom<N>

§

impl<const N: usize> RefUnwindSafe for FixedRandom<N>

§

impl<const N: usize> Send for FixedRandom<N>

§

impl<const N: usize> Sync for FixedRandom<N>

§

impl<const N: usize> Unpin for FixedRandom<N>

§

impl<const N: usize> UnwindSafe for FixedRandom<N>

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