64 lines
11 KiB
HTML
64 lines
11 KiB
HTML
|
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `getrandom` crate."><meta name="keywords" content="rust, rustlang, rust-lang, getrandom"><title>getrandom - Rust</title><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../dark.css"><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script src="../storage.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="shortcut icon" href="https://www.rust-lang.org/favicon.ico"><style type="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../getrandom/index.html'><div class='logo-container'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk.png' alt='logo'></div></a><p class='location'>Crate getrandom</p><div class="sidebar-elems"><a id='all-types' href='all.html'><p>See all getrandom's items</p></a><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'getrandom', ty: 'mod', relpath: '../'};</script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../settings.html"><img src="../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='../src/getrandom/lib.rs.html#9-256' title='goto source code'>[src]</a></span><span class='in-band'>Crate <a class="mod" href=''>getrandom</a></span></h1><div class='docblock'><p>Interface to the random number generator of the operating system.</p>
|
|||
|
<h1 id="platform-sources" class="section-header"><a href="#platform-sources">Platform sources</a></h1><table><thead><tr><th>OS</th><th>interface</th></tr></thead><tbody>
|
|||
|
<tr><td>Linux, Android</td><td><a href="http://man7.org/linux/man-pages/man2/getrandom.2.html"><code>getrandom</code></a> system call if available, otherwise <a href="http://man7.org/linux/man-pages/man4/urandom.4.html"><code>/dev/urandom</code></a> after reading from <code>/dev/random</code> once</td></tr>
|
|||
|
<tr><td>Windows</td><td><a href="https://docs.microsoft.com/en-us/windows/desktop/api/ntsecapi/nf-ntsecapi-rtlgenrandom"><code>RtlGenRandom</code></a></td></tr>
|
|||
|
<tr><td>macOS</td><td><a href="https://www.unix.com/man-page/mojave/2/getentropy/"><code>getentropy()</code></a> if available, otherwise <a href="https://www.unix.com/man-page/mojave/4/random/"><code>/dev/random</code></a> (identical to <code>/dev/urandom</code>)</td></tr>
|
|||
|
<tr><td>iOS</td><td><a href="https://developer.apple.com/documentation/security/1399291-secrandomcopybytes?language=objc"><code>SecRandomCopyBytes</code></a></td></tr>
|
|||
|
<tr><td>FreeBSD</td><td><a href="https://www.freebsd.org/cgi/man.cgi?query=getrandom&manpath=FreeBSD+12.0-stable"><code>getrandom()</code></a> if available, otherwise <a href="https://www.freebsd.org/cgi/man.cgi?query=random&sektion=4"><code>kern.arandom</code></a></td></tr>
|
|||
|
<tr><td>OpenBSD</td><td><a href="https://man.openbsd.org/getentropy.2"><code>getentropy</code></a></td></tr>
|
|||
|
<tr><td>NetBSD</td><td><a href="http://netbsd.gw.com/cgi-bin/man-cgi?random+4+NetBSD-current"><code>/dev/urandom</code></a> after reading from <code>/dev/random</code> once</td></tr>
|
|||
|
<tr><td>Dragonfly BSD</td><td><a href="https://leaf.dragonflybsd.org/cgi/web-man?command=random&section=4"><code>/dev/random</code></a></td></tr>
|
|||
|
<tr><td>Solaris, illumos</td><td><a href="https://docs.oracle.com/cd/E88353_01/html/E37841/getrandom-2.html"><code>getrandom</code></a> system call if available, otherwise <a href="https://docs.oracle.com/cd/E86824_01/html/E54777/random-7d.html"><code>/dev/random</code></a></td></tr>
|
|||
|
<tr><td>Fuchsia OS</td><td><a href="https://fuchsia.googlesource.com/fuchsia/+/master/zircon/docs/syscalls/cprng_draw.md"><code>cprng_draw</code></a></td></tr>
|
|||
|
<tr><td>Redox</td><td><a href="https://github.com/redox-os/randd/blob/master/src/main.rs"><code>rand:</code></a></td></tr>
|
|||
|
<tr><td>CloudABI</td><td><a href="https://github.com/nuxinl/cloudabi#random_get"><code>cloudabi_sys_random_get</code></a></td></tr>
|
|||
|
<tr><td>Haiku</td><td><code>/dev/random</code> (identical to <code>/dev/urandom</code>)</td></tr>
|
|||
|
<tr><td>L4RE, SGX, UEFI</td><td><a href="https://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide">RDRAND</a></td></tr>
|
|||
|
<tr><td>Hermit</td><td><a href="https://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide">RDRAND</a> as <a href="https://github.com/hermitcore/libhermit-rs/blob/09c38b0371cee6f56a541400ba453e319e43db53/src/syscalls/random.rs#L21"><code>sys_rand</code></a> is currently broken.</td></tr>
|
|||
|
<tr><td>Web browsers</td><td><a href="https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues"><code>Crypto.getRandomValues</code></a> (see <a href="https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues">Support for WebAssembly and ams.js</a>)</td></tr>
|
|||
|
<tr><td>Node.js</td><td><a href="https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback"><code>crypto.randomBytes</code></a> (see <a href="#support-for-webassembly-and-amsjs">Support for WebAssembly and ams.js</a>)</td></tr>
|
|||
|
<tr><td>WASI</td><td><a href="https://github.com/WebAssembly/WASI/blob/master/design/WASI-core.md#__wasi_random_get"><code>__wasi_random_get</code></a></td></tr>
|
|||
|
</tbody></table>
|
|||
|
<p>Getrandom doesn't have a blanket implementation for all Unix-like operating
|
|||
|
systems that reads from <code>/dev/urandom</code>. This ensures all supported operating
|
|||
|
systems are using the recommended interface and respect maximum buffer
|
|||
|
sizes.</p>
|
|||
|
<h2 id="support-for-webassembly-and-amsjs" class="section-header"><a href="#support-for-webassembly-and-amsjs">Support for WebAssembly and ams.js</a></h2>
|
|||
|
<p>The three Emscripten targets <code>asmjs-unknown-emscripten</code>,
|
|||
|
<code>wasm32-unknown-emscripten</code> and <code>wasm32-experimental-emscripten</code> use
|
|||
|
Emscripten's emulation of <code>/dev/random</code> on web browsers and Node.js.</p>
|
|||
|
<p>The bare WASM target <code>wasm32-unknown-unknown</code> tries to call the javascript
|
|||
|
methods directly, using either <code>stdweb</code> or <code>wasm-bindgen</code> depending on what
|
|||
|
features are activated for this crate. Note that if both features are
|
|||
|
enabled <code>wasm-bindgen</code> will be used. If neither feature is enabled,
|
|||
|
<code>getrandom</code> will always fail.</p>
|
|||
|
<p>The WASI target <code>wasm32-wasi</code> uses the <code>__wasi_random_get</code> function defined
|
|||
|
by the WASI standard.</p>
|
|||
|
<h2 id="early-boot" class="section-header"><a href="#early-boot">Early boot</a></h2>
|
|||
|
<p>It is possible that early in the boot process the OS hasn't had enough time
|
|||
|
yet to collect entropy to securely seed its RNG, especially on virtual
|
|||
|
machines.</p>
|
|||
|
<p>Some operating systems always block the thread until the RNG is securely
|
|||
|
seeded. This can take anywhere from a few seconds to more than a minute.
|
|||
|
Others make a best effort to use a seed from before the shutdown and don't
|
|||
|
document much.</p>
|
|||
|
<p>A few, Linux, NetBSD and Solaris, offer a choice between blocking and
|
|||
|
getting an error; in these cases we always choose to block.</p>
|
|||
|
<p>On Linux (when the <code>genrandom</code> system call is not available) and on NetBSD
|
|||
|
reading from <code>/dev/urandom</code> never blocks, even when the OS hasn't collected
|
|||
|
enough entropy yet. To avoid returning low-entropy bytes, we first read from
|
|||
|
<code>/dev/random</code> and only switch to <code>/dev/urandom</code> once this has succeeded.</p>
|
|||
|
<h1 id="error-handling" class="section-header"><a href="#error-handling">Error handling</a></h1>
|
|||
|
<p>We always choose failure over returning insecure "random" bytes. In general,
|
|||
|
on supported platforms, failure is highly unlikely, though not impossible.
|
|||
|
If an error does occur, then it is likely that it will occur on every call to
|
|||
|
<code>getrandom</code>, hence after the first successful call one can be reasonably
|
|||
|
confident that no errors will occur.</p>
|
|||
|
<p>On unsupported platforms, <code>getrandom</code> always fails. See the [<code>Error</code>] type
|
|||
|
for more information on what data is returned on failure.</p>
|
|||
|
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
|
|||
|
<table><tr class='module-item'><td><a class="struct" href="struct.Error.html" title='getrandom::Error struct'>Error</a></td><td class='docblock-short'><p>A small and <code>no_std</code> compatible error type.</p>
|
|||
|
</td></tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2>
|
|||
|
<table><tr class='module-item'><td><a class="fn" href="fn.getrandom.html" title='getrandom::getrandom fn'>getrandom</a></td><td class='docblock-short'><p>Fill <code>dest</code> with random bytes from the system's preferred random number
|
|||
|
source.</p>
|
|||
|
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>⏎</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g., <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g., <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../";window.currentCrate = "getrandom";</script><script src="../aliases.js"></script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html>
|