Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub mod io;
pub mod iter;
#[cfg(all(target_os = "wasi", target_env = "p2"))]
pub mod net;
#[cfg(all(target_os = "wasi", target_env = "p2"))]
#[cfg(target_os = "wasi")]
pub mod rand;
#[cfg(all(target_os = "wasi", target_env = "p2"))]
pub mod runtime;
Expand Down
3 changes: 3 additions & 0 deletions src/rand/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Random number generation.

#[cfg(target_env = "p2")]
use wasip2::random;
#[cfg(target_env = "p3")]
use wasip3::random;

/// Fill the slice with cryptographically secure random bytes.
pub fn get_random_bytes(buf: &mut [u8]) {
Expand Down
14 changes: 14 additions & 0 deletions tests/rand.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// Check that the rand interface works with empty and non-empty buffers.
#[wstd::test]
async fn fills_random_bytes() {
let mut secure = [0; 32];
wstd::rand::get_random_bytes(&mut secure);
assert!(secure.iter().any(|byte| *byte != 0));

let mut insecure = [0; 32];
wstd::rand::get_insecure_random_bytes(&mut insecure);
assert!(insecure.iter().any(|byte| *byte != 0));

wstd::rand::get_random_bytes(&mut []);
wstd::rand::get_insecure_random_bytes(&mut []);
}
Loading