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.js require("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]

[src]

Create a new OsRng.

Trait Implementations

impl Debug for OsRng
[src]

[src]

Formats the value using the given formatter. Read more

impl RngCore for OsRng
[src]

[src]

Return the next random u32. Read more

[src]

Return the next random u64. Read more

[src]

Fill dest with random data. Read more

[src]

Fill dest entirely with random data. Read more

Auto Trait Implementations

impl Send for OsRng

impl Sync for OsRng