pub struct FixedRng<const N: usize>(/* private fields */);Expand description
Fixed-length cryptographically secure random value.
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.
§Examples
Basic usage:
use secure_gate::random::FixedRng;
let random: FixedRng<32> = FixedRng::generate();
assert_eq!(random.len(), 32);With alias:
use secure_gate::fixed_alias_rng;
fixed_alias_rng!(Nonce, 24);
let nonce = Nonce::generate();Implementations§
Source§impl<const N: usize> FixedRng<N>
impl<const N: usize> FixedRng<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::FixedRng;
let random = FixedRng::<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::FixedRng;
let random: Result<FixedRng<32>, rand::rand_core::OsError> = FixedRng::try_generate();
assert!(random.is_ok());Sourcepub fn expose_secret(&self) -> &[u8; N]
pub fn expose_secret(&self) -> &[u8; N]
Expose the random bytes for read-only access.
§Example
use secure_gate::random::FixedRng;
let random = FixedRng::<4>::generate();
let bytes = random.expose_secret();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::FixedRng};
let random = FixedRng::<32>::generate();
let fixed: Fixed<[u8; 32]> = random.into_inner();
// Can now use fixed.expose_secret() as neededTrait Implementations§
Source§impl<const N: usize> From<FixedRng<N>> for Fixed<[u8; N]>
impl<const N: usize> From<FixedRng<N>> for Fixed<[u8; N]>
Source§fn from(rng: FixedRng<N>) -> Self
fn from(rng: FixedRng<N>) -> Self
Convert a FixedRng to Fixed, transferring ownership.
This preserves all security guarantees. The FixedRng 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::FixedRng};
let key: Fixed<[u8; 32]> = FixedRng::<32>::generate().into();