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, ExposeSecret};
let random: FixedRandom<32> = FixedRandom::generate();
assert_eq!(random.len(), 32);With alias:
use secure_gate::ExposeSecret;
use secure_gate::fixed_alias_random;
fixed_alias_random!(Nonce, 24);
let nonce = Nonce::generate();Implementations§
Source§impl<const N: usize> FixedRandom<N>
impl<const N: usize> FixedRandom<N>
Sourcepub fn generate() -> Self
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, ExposeSecret};
let random = FixedRandom::<16>::generate();
assert!(!random.is_empty());Sourcepub fn try_generate() -> Result<Self, OsError>
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());Sourcepub fn into_inner(self) -> Fixed<[u8; N]>
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 neededSource§impl<const N: usize> FixedRandom<N>
impl<const N: usize> FixedRandom<N>
Source§impl<const N: usize> FixedRandom<N>
impl<const N: usize> FixedRandom<N>
Sourcepub fn to_base64url(&self) -> Base64String
pub fn to_base64url(&self) -> Base64String
Borrowing encode (original random remains usable).
Sourcepub fn into_base64url(self) -> Base64String
pub fn into_base64url(self) -> Base64String
Consuming encode (raw random bytes zeroized immediately).
Source§impl<const N: usize> FixedRandom<N>
impl<const N: usize> FixedRandom<N>
Sourcepub fn try_to_bech32(
&self,
hrp: &str,
) -> Result<Bech32String, Bech32EncodingError>
pub fn try_to_bech32( &self, hrp: &str, ) -> Result<Bech32String, Bech32EncodingError>
Borrowing encode (original random remains usable).
pub fn try_to_bech32m( &self, hrp: &str, ) -> Result<Bech32String, Bech32EncodingError>
Sourcepub fn try_into_bech32(
self,
hrp: &str,
) -> Result<Bech32String, Bech32EncodingError>
pub fn try_into_bech32( self, hrp: &str, ) -> Result<Bech32String, Bech32EncodingError>
Consuming encode (raw random bytes zeroized immediately).
pub fn try_into_bech32m( self, hrp: &str, ) -> Result<Bech32String, Bech32EncodingError>
Trait Implementations§
Source§impl<const N: usize> Debug for FixedRandom<N>
Debug implementation (always redacted).
impl<const N: usize> Debug for FixedRandom<N>
Debug implementation (always redacted).
Source§impl<const N: usize> ExposeSecret for FixedRandom<N>
Available on crate feature rand only.Implementation for FixedRandom<N> - read-only access.
impl<const N: usize> ExposeSecret for FixedRandom<N>
rand only.Implementation for FixedRandom<N> - 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§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();impl<const N: usize> SecureRandom for FixedRandom<N>
rand only.