README.md
1# getrandom
2
3[![Build Status](https://travis-ci.org/rust-random/getrandom.svg?branch=master)](https://travis-ci.org/rust-random/getrandom)
4[![Build Status](https://ci.appveyor.com/api/projects/status/github/rust-random/getrandom?svg=true)](https://ci.appveyor.com/project/rust-random/getrandom)
5[![Crate](https://img.shields.io/crates/v/getrandom.svg)](https://crates.io/crates/getrandom)
6[![Documentation](https://docs.rs/getrandom/badge.svg)](https://docs.rs/getrandom)
7[![Dependency status](https://deps.rs/repo/github/rust-random/getrandom/status.svg)](https://deps.rs/repo/github/rust-random/getrandom)
8
9
10A Rust library for retrieving random data from (operating) system source. It is
11assumed that system always provides high-quality cryptographically secure random
12data, ideally backed by hardware entropy sources. This crate derives its name
13from Linux's `getrandom` function, but is cross platform, roughly supporting
14the same set of platforms as Rust's `std` lib.
15
16This is a low-level API. Most users should prefer using high-level random-number
17library like [`rand`].
18
19[`rand`]: https://crates.io/crates/rand
20
21## Usage
22
23Add this to your `Cargo.toml`:
24
25```toml
26[dependencies]
27getrandom = "0.1"
28```
29
30Then invoke the `getrandom` function:
31
32```rust
33fn get_random_buf() -> Result<[u8; 32], getrandom::Error> {
34 let mut buf = [0u8; 32];
35 getrandom::getrandom(&mut buf)?;
36 Ok(buf)
37}
38```
39
40## Features
41
42This library is `no_std` for every supported target. However, getting randomness
43usually requires calling some external system API. This means most platforms
44will require linking against system libraries (i.e. `libc` for Unix,
45`Advapi32.dll` for Windows, Security framework on iOS, etc...).
46
47The `log` library is supported as an optional dependency. If enabled, error
48reporting will be improved on some platforms.
49
50For the `wasm32-unknown-unknown` target, one of the following features should be
51enabled:
52
53- [`wasm-bindgen`](https://crates.io/crates/wasm_bindgen)
54- [`stdweb`](https://crates.io/crates/stdweb)
55
56By default, compiling `getrandom` for an unsupported target will result in
57a compilation error. If you want to build an application which uses `getrandom`
58for such target, you can either:
59- Use [`[replace]`][replace] or [`[patch]`][patch] section in your `Cargo.toml`
60to switch to a custom implementation with a support of your target.
61- Enable the `dummy` feature to have getrandom use an implementation that always
62fails at run-time on unsupported targets.
63
64[replace]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-replace-section
65[patch]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-patch-section
66
67## Minimum Supported Rust Version
68
69This crate requires Rust 1.32.0 or later.
70
71# License
72
73The `getrandom` library is distributed under either of
74
75 * [Apache License, Version 2.0](LICENSE-APACHE)
76 * [MIT license](LICENSE-MIT)
77
78at your option.
79