mb_rand

Crates.iomb_rand
lib.rsmb_rand
version0.2.0
created_at2025-05-27 17:29:38.884519+00
updated_at2025-05-27 17:29:38.884519+00
descriptionSafe Rust bindings to OpenBSD's arc4random functions
homepagehttps://mb-rand.microbio.rs
repositoryhttps://git.sr.ht/~microbio/mb-rand
max_upload_size
id1691385
size19,732
ijanc (murilobsd)

documentation

README

mb-rand

A Rust library crate providing safe bindings to OpenBSD's arc4random functions.

Features

  • fill_bytes - Fill a byte array with cryptographically secure random data
  • random_bytes - Generate a new vector filled with random bytes
  • random_string - Generate a random string from a character set

Usage

Add this to your Cargo.toml:

[dependencies]
mb_rand = "0.2.0"

Examples

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);
}

Requirements

This crate relies on the host system having OpenBSD's [arc4random][arc4random_man] functions available. This is typically the case on:

  • OpenBSD (native)
  • FreeBSD (compatible implementation)
  • macOS (compatible implementation)
  • Some Linux distributions with libbsd installed

On systems where these functions are not available, you will need to install the appropriate libraries:

Ubuntu/Debian

sudo apt-get install libbsd-dev

Fedora/RHEL/CentOS

sudo dnf install libbsd-devel

Platform-Specific Configuration

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");
}

Safety

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.

License

Licensed under an OpenBSD-ISC-style license, see LICENSE for details.

Commit count: 0

cargo fmt