Skip to content
CryoCryo home
Stdlibrandom

secure

import std::random::secure; · source

Types

SecureRng

type struct SecureRng {
    static new() -> SecureRng;
    try_fill(&this, dst: Slice<u8>) -> Result<(), RandomError>;
}
import std::random::secure;

mut s: SecureRng = SecureRng::new();
s.fill(key.as_slice());

Because it satisfies RandomSource, the full draw surface above is available. Those defaults panic if the kernel cannot supply entropy — a broken system invariant rather than an expected failure. When you need to handle it, use try_fill, which reports it as EntropyUnavailable.

SecureRng is stateless, so a copy is the same source. The backing call is per target: getrandom on Unix, RtlGenRandom on Windows. Rng::from_os routes through the same portable helper rather than binding the Linux-only symbol directly.

Constructors

static new() -> SecureRng;

A handle to the kernel CSPRNG. Holds no state.

Methods

try_fill(&this, dst: Slice<u8>) -> Result<(), RandomError>;

Fill dst with secure bytes, reporting EntropyUnavailable if the kernel source cannot satisfy the request. The careful counterpart to the RandomSource::fill default (which panics on failure). On Ok the whole view is written.

Trait implementations

implement trait RandomSource for struct SecureRng

Its RandomSource impl overrides fill so a bulk request is one kernel call rather than a loop of next_64.