| Joel Galenson | 4be0c6d | 2020-07-07 13:20:14 -0700 | [diff] [blame^] | 1 | # getrandom |
| 2 | |
| 3 | [](https://travis-ci.org/rust-random/getrandom) |
| 4 | [](https://ci.appveyor.com/project/rust-random/getrandom) |
| 5 | [](https://crates.io/crates/getrandom) |
| 6 | [](https://docs.rs/getrandom) |
| 7 | [](https://deps.rs/repo/github/rust-random/getrandom) |
| 8 | |
| 9 | |
| 10 | A Rust library for retrieving random data from (operating) system source. It is |
| 11 | assumed that system always provides high-quality cryptographically secure random |
| 12 | data, ideally backed by hardware entropy sources. This crate derives its name |
| 13 | from Linux's `getrandom` function, but is cross platform, roughly supporting |
| 14 | the same set of platforms as Rust's `std` lib. |
| 15 | |
| 16 | This is a low-level API. Most users should prefer using high-level random-number |
| 17 | library like [`rand`]. |
| 18 | |
| 19 | [`rand`]: https://crates.io/crates/rand |
| 20 | |
| 21 | ## Usage |
| 22 | |
| 23 | Add this to your `Cargo.toml`: |
| 24 | |
| 25 | ```toml |
| 26 | [dependencies] |
| 27 | getrandom = "0.1" |
| 28 | ``` |
| 29 | |
| 30 | Then invoke the `getrandom` function: |
| 31 | |
| 32 | ```rust |
| 33 | fn 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 | |
| 42 | This library is `no_std` for every supported target. However, getting randomness |
| 43 | usually requires calling some external system API. This means most platforms |
| 44 | will require linking against system libraries (i.e. `libc` for Unix, |
| 45 | `Advapi32.dll` for Windows, Security framework on iOS, etc...). |
| 46 | |
| 47 | The `log` library is supported as an optional dependency. If enabled, error |
| 48 | reporting will be improved on some platforms. |
| 49 | |
| 50 | For the `wasm32-unknown-unknown` target, one of the following features should be |
| 51 | enabled: |
| 52 | |
| 53 | - [`wasm-bindgen`](https://crates.io/crates/wasm_bindgen) |
| 54 | - [`stdweb`](https://crates.io/crates/stdweb) |
| 55 | |
| 56 | By default, compiling `getrandom` for an unsupported target will result in |
| 57 | a compilation error. If you want to build an application which uses `getrandom` |
| 58 | for such target, you can either: |
| 59 | - Use [`[replace]`][replace] or [`[patch]`][patch] section in your `Cargo.toml` |
| 60 | to switch to a custom implementation with a support of your target. |
| 61 | - Enable the `dummy` feature to have getrandom use an implementation that always |
| 62 | fails 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 | |
| 69 | This crate requires Rust 1.32.0 or later. |
| 70 | |
| 71 | # License |
| 72 | |
| 73 | The `getrandom` library is distributed under either of |
| 74 | |
| 75 | * [Apache License, Version 2.0](LICENSE-APACHE) |
| 76 | * [MIT license](LICENSE-MIT) |
| 77 | |
| 78 | at your option. |