2019-01-19 12:56:20 +00:00
|
|
|
# Rand
|
|
|
|
|
|
|
|
[](https://travis-ci.org/rust-random/getrandom)
|
|
|
|
[](https://ci.appveyor.com/project/rust-random/getrandom)
|
|
|
|
[](https://crates.io/crates/getrandom)
|
|
|
|
[](https://docs.rs/getrandom)
|
|
|
|
|
|
|
|
A Rust library to securely get random entropy. This crate derives its name from
|
|
|
|
Linux's `getrandom` function, but is cross platform, roughly supporting the same
|
|
|
|
set of platforms as Rust's `std` lib.
|
|
|
|
|
2019-02-18 12:09:23 +00:00
|
|
|
This is a low-level API. Most users should prefer a high-level random-number
|
|
|
|
library like [Rand] or a cryptography library.
|
|
|
|
|
|
|
|
[Rand]: https://crates.io/crates/rand
|
|
|
|
|
2019-01-19 12:56:20 +00:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
Add this to your `Cargo.toml`:
|
|
|
|
|
|
|
|
```toml
|
|
|
|
[dependencies]
|
|
|
|
getrandom = "0.1"
|
|
|
|
```
|
|
|
|
|
2019-02-18 12:09:23 +00:00
|
|
|
Then invoke the `getrandom` function:
|
|
|
|
|
|
|
|
```rust
|
|
|
|
fn get_random_buf() -> Result<[u8; 32], getrandom::Error> {
|
|
|
|
let mut buf = [0u8; 32];
|
|
|
|
getrandom::getrandom(&mut buf)?;
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
```
|
2019-01-19 12:56:20 +00:00
|
|
|
|
2019-02-18 14:38:53 +00:00
|
|
|
## Features
|
|
|
|
|
|
|
|
This library is `no_std` compatible on SGX but requires `std` on most platforms.
|
|
|
|
|
2019-03-12 17:39:20 +00:00
|
|
|
The `log` library is supported as an optional dependency. If enabled, error
|
|
|
|
reporting will be improved on some platforms.
|
|
|
|
|
2019-02-18 14:38:53 +00:00
|
|
|
For WebAssembly (`wasm32`), Enscripten targets are supported directly; otherwise
|
|
|
|
one of the following features must be enabled:
|
|
|
|
|
|
|
|
- [`wasm-bindgen`](https://crates.io/crates/wasm_bindgen)
|
|
|
|
- [`stdweb`](https://crates.io/crates/stdweb)
|
|
|
|
|
2019-02-25 10:26:48 +00:00
|
|
|
## Versions
|
|
|
|
|
|
|
|
This crate requires Rustc version 1.28.0 or later due to usage of `NonZeroU32`.
|
|
|
|
|
2019-01-19 12:56:20 +00:00
|
|
|
|
|
|
|
# License
|
|
|
|
|
|
|
|
The `getrandom` library is distributed under the terms of both the MIT license
|
|
|
|
and the Apache License (Version 2.0).
|
|
|
|
|
|
|
|
See [LICENSE-APACHE](LICENSE-APACHE) and [LICENSE-MIT](LICENSE-MIT), and
|
|
|
|
[COPYRIGHT](COPYRIGHT) for details.
|