pub struct SystemRng;random)Expand description
The system random number generator.
This asks the system for random data suitable for cryptographic purposes such as key generation. If security is a concern, consult the platform documentation below for the specific guarantees your target provides.
The high quality of randomness provided by this source means it can be quite
slow on some targets. If you need a large quantity of random numbers and
security is not a concern, you might want to consider using an alternative
random number generator. That said, std attempts to use the fastest source
available on the system that still provides strong security. A custom random
number generator will in nearly all cases have weaker security attributes
than SystemRng.
ยงBlocking
The underlying syscalls might block the calling thread until there is sufficient entropy in the system to seed a procedural random number generator. This is usually only a concern if the program runs immediately after the system boots.
ยงPanicking
Calling fill_bytes will panic if the system cannot provide the required
random data. Due to the blocking behaviour described above, this is a
permanent condition in nearly all cases and implies that the system lacks
the facilities (hardware or software) necessary for collecting entropy.
Non-embedded systems usually do not suffer from this limitation.
ยงUnderlying sources
| Platform | Source |
|---|---|
| Linux | getrandom or /dev/urandom after polling /dev/random |
| Windows | ProcessPrng |
| Apple | CCRandomGenerateBytes |
| DragonFly | arc4random_buf |
| ESP-IDF | esp_fill_random |
| FreeBSD | arc4random_buf |
| Fuchsia | cprng_draw |
| Haiku | arc4random_buf |
| Illumos | arc4random_buf |
| NetBSD | arc4random_buf |
| OpenBSD | arc4random_buf |
| Solaris | arc4random_buf |
| Vita | arc4random_buf |
| Hermit | read_entropy |
| Horizon, Cygwin | getrandom |
| AIX, Hurd, QNX | /dev/urandom |
| Redox | /scheme/rand |
| RTEMS | arc4random_buf |
| SGX | rdrand |
| SOLID | SOLID_RNG_SampleRandomBytes |
| TEEOS | TEE_GenerateRandom |
| UEFI | EFI_RNG_PROTOCOL |
| VxWorks | randABytes after waiting for randSecure to become ready |
| WASIp1 | random_get |
| WASIp2 | get-random-bytes |
| WASIp3 | get-random-bytes |
| ZKVM | sys_rand |
Note that the sources used might change over time.
Consult the documentation for the underlying operations on your supported targets to determine whether they provide any particular desired properties, such as support for reseeding on VM fork operations.
ยงExamples
Filling a buffer with random bytes
#![feature(random)]
use std::random::{Rng, SystemRng};
let mut buf = [0; 16];
SystemRng.fill_bytes(&mut buf);
dbg!(buf);Trait Implementationsยง
impl Copy for SystemRng
Auto Trait Implementationsยง
impl Freeze for SystemRng
impl RefUnwindSafe for SystemRng
impl Send for SystemRng
impl Sync for SystemRng
impl Unpin for SystemRng
impl UnsafeUnpin for SystemRng
impl UnwindSafe for SystemRng
Blanket Implementationsยง
Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Sourceยงimpl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Sourceยงimpl<T> Instrument for T
impl<T> Instrument for T
Sourceยงfn instrument(self, span: Span) -> Instrumented<Self> โ
fn instrument(self, span: Span) -> Instrumented<Self> โ
Sourceยงfn in_current_span(self) -> Instrumented<Self> โ
fn in_current_span(self) -> Instrumented<Self> โ
Sourceยงimpl<T> IntoEither for T
impl<T> IntoEither for T
Sourceยงfn into_either(self, into_left: bool) -> Either<Self, Self> โ
fn into_either(self, into_left: bool) -> Either<Self, Self> โ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSourceยงfn into_either_with<F>(self, into_left: F) -> Either<Self, Self> โ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> โ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSourceยงimpl<T> Pointable for T
impl<T> Pointable for T
Sourceยงimpl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Sourceยงimpl<T> PowerToOwned for T
impl<T> PowerToOwned for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Sourceยงimpl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Sourceยงfn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().