-
Notifications
You must be signed in to change notification settings - Fork 239
Add OsRng to getrandom #751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dhardy
wants to merge
15
commits into
master
Choose a base branch
from
push-tqlwtzsrywpy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−16
Open
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d4a480a
Add rand_core as an optional dependency
dhardy d2f6a1d
Add OsRng
dhardy bdbe38d
Test rng feature in CI
dhardy 26cb7e9
Test std / rng / std,rng feature combinations
dhardy 254be58
Use rng feature in docs builds
dhardy ff68f2a
Use test module
dhardy 8bdcacf
pub use rand_core
dhardy 1e873b4
Document what OsRng implements
dhardy b7ed994
Rename OsRng -> SysRng
dhardy c87ed80
Rename feature rng -> sys_rng
dhardy c304fa3
Move SysRng tests to tests/ folder
dhardy fac8d43
Remove redundant test
dhardy 9982171
Rename more things to sys_rng
dhardy 0ce5882
Rename modules to sys_rng
dhardy fac5045
Move sys_rng module descriptor
dhardy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| //! rand_core adapter | ||
| use crate::Error; | ||
| use rand_core::{TryCryptoRng, TryRngCore}; | ||
|
|
||
| /// An RNG over the operating-system's random data source | ||
| /// | ||
| /// This is a zero-sized struct. It can be freely constructed with just `SysRng`. | ||
| /// | ||
| /// This struct is also available as [`rand::rngs::SysRng`] when using [rand]. | ||
| /// | ||
| /// # Usage example | ||
| /// | ||
| /// `SysRng` implements [`TryRngCore`]: | ||
| /// ``` | ||
| /// use getrandom::{rand_core::TryRngCore, SysRng}; | ||
| /// | ||
| /// let mut key = [0u8; 32]; | ||
| /// SysRng.try_fill_bytes(&mut key).unwrap(); | ||
| /// ``` | ||
| /// | ||
| /// Using it as an [`RngCore`] is possible using [`TryRngCore::unwrap_err`]: | ||
| /// ``` | ||
| /// use getrandom::rand_core::{TryRngCore, RngCore}; | ||
| /// use getrandom::SysRng; | ||
| /// | ||
| /// let mut rng = SysRng.unwrap_err(); | ||
| /// let random_u64 = rng.next_u64(); | ||
| /// ``` | ||
| /// | ||
| /// [rand]: https://crates.io/crates/rand | ||
| /// [`rand::rngs::SysRng`]: https://docs.rs/rand/latest/rand/rngs/struct.SysRng.html | ||
| /// [`RngCore`]: rand_core::RngCore | ||
| #[derive(Clone, Copy, Debug, Default)] | ||
| pub struct SysRng; | ||
|
|
||
| impl TryRngCore for SysRng { | ||
| type Error = Error; | ||
|
|
||
| #[inline] | ||
| fn try_next_u32(&mut self) -> Result<u32, Error> { | ||
| crate::u32() | ||
| } | ||
|
|
||
| #[inline] | ||
| fn try_next_u64(&mut self) -> Result<u64, Error> { | ||
| crate::u64() | ||
| } | ||
|
|
||
| #[inline] | ||
| fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { | ||
| crate::fill(dest) | ||
| } | ||
| } | ||
|
|
||
| impl TryCryptoRng for SysRng {} | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
newpavlov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
newpavlov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_os_rng() { | ||
| let x = SysRng.try_next_u64().unwrap(); | ||
| let y = SysRng.try_next_u64().unwrap(); | ||
| assert!(x != 0); | ||
| assert!(x != y); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_construction() { | ||
| assert!(SysRng.try_next_u64().unwrap() != 0); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #![cfg(feature = "sys_rng")] | ||
|
|
||
| use getrandom::SysRng; | ||
| use getrandom::rand_core::TryRngCore; | ||
|
|
||
| #[test] | ||
| fn test_os_rng() { | ||
newpavlov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let x = SysRng.try_next_u64().unwrap(); | ||
| let y = SysRng.try_next_u64().unwrap(); | ||
| assert!(x != 0); | ||
| assert!(x != y); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_construction() { | ||
| assert!(SysRng.try_next_u64().unwrap() != 0); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.