Struct rand::os::OsRng
[−]
[src]
pub struct OsRng(_);
A random number generator that retrieves randomness straight from the
operating system. This is the preferred external source of entropy for most
applications. Commonly it is used to initialize a user-space RNG, which can
then be used to generate random values with much less overhead than OsRng
.
You may prefer to use EntropyRng
instead of OsRng
. Is is unlikely, but
not entirely theoretical, for OsRng
to fail. In such cases EntropyRng
falls back on a good alternative entropy source.
OsRng
usually does not block. On some systems, and notably virtual
machines, it may block very early in the init process, when the OS CSPRNG
has not yet been seeded.
OsRng::new()
is guaranteed to be very cheap (after first call), and will
never consume more than one file handle per process.
Platform sources:
- Linux, Android: reads from the
getrandom(2)
system call if available, otherwise from/dev/urandom
. - macOS, iOS: calls
SecRandomCopyBytes
. - Windows: calls
RtlGenRandom
. - WASM: calls
window.crypto.getRandomValues
in browsers, and in Node.jsrequire("crypto").randomBytes
. - OpenBSD: calls
getentropy(2)
. - FreeBSD: uses the
kern.arandom
sysctl(2)
mib. - Fuchsia: calls
cprng_draw
. - Redox: reads from
rand:
device. - CloudABI: calls
random_get
. - Other Unix-like systems: reads directly from
/dev/urandom
. Note: many Unix systems provide/dev/random
as well as/dev/urandom
. On all modern systems these two interfaces offer identical quality, with the difference that on some systems/dev/random
may block. This is a dated design, and/dev/urandom
is preferred by cryptography experts. [1]
[1] See Myths about urandom.
Methods
impl OsRng
[src]
Trait Implementations
impl Debug for OsRng
[src]
fn fmt(&self, f: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
impl RngCore for OsRng
[src]
fn next_u32(&mut self) -> u32
[src]
Return the next random u32
. Read more
fn next_u64(&mut self) -> u64
[src]
Return the next random u64
. Read more
fn fill_bytes(&mut self, dest: &mut [u8])
[src]
Fill dest
with random data. Read more
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>
[src]
Fill dest
entirely with random data. Read more