Crates.io | mb_rand |
lib.rs | mb_rand |
version | 0.2.0 |
created_at | 2025-05-27 17:29:38.884519+00 |
updated_at | 2025-05-27 17:29:38.884519+00 |
description | Safe Rust bindings to OpenBSD's arc4random functions |
homepage | https://mb-rand.microbio.rs |
repository | https://git.sr.ht/~microbio/mb-rand |
max_upload_size | |
id | 1691385 |
size | 19,732 |
A Rust library crate providing safe bindings to OpenBSD's arc4random functions.
fill_bytes
- Fill a byte array with cryptographically secure random datarandom_bytes
- Generate a new vector filled with random bytesrandom_string
- Generate a random string from a character setAdd this to your Cargo.toml
:
[dependencies]
mb_rand = "0.2.0"
Fill a byte array with random data:
use mb_rand::fill_bytes;
fn main() {
let mut buffer = [0u8; 16];
fill_bytes(&mut buffer);
println!("Random bytes: {:?}", buffer);
}
Generate a random string:
use mb_rand::random_string;
fn main() {
// Generate a random password with letters and numbers
let charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let password = random_string(charset, 16);
println!("Random password: {}", password);
}
This crate relies on the host system having OpenBSD's
[arc4random][arc4random_man]
functions available. This is typically the case
on:
On systems where these functions are not available, you will need to install the appropriate libraries:
sudo apt-get install libbsd-dev
sudo dnf install libbsd-devel
For systems where the arc4random functions need explicit
linking, you may need to create a build script (build.rs
):
fn main() {
println!("cargo:rustc-link-lib=bsd");
}
This crate provides safe wrappers around the unsafe FFI calls to the arc4random functions. The underlying arc4random implementation is considered cryptographically secure and suitable for generating random data for security-sensitive applications.
Licensed under an OpenBSD-ISC-style license, see LICENSE for details.