mb_rand 0.2.0

Safe Rust bindings to OpenBSD's arc4random functions
Documentation
//
// Copyright (c) 2025 murilo ijanc' <mbsd@m0x.ru>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
use mb_rand::{fill_bytes, random_bytes, random_string};
use std::env;

fn main() {
    // Get the password length from command-line arguments or use a default
    let length =
        env::args().nth(1).and_then(|s| s.parse::<usize>().ok()).unwrap_or(16);

    // Example 1: Fill a byte array with random data
    let mut buffer = vec![0u8; length];
    fill_bytes(&mut buffer);
    println!("1. Random bytes: {:?}", buffer);

    // Example 2: Generate random bytes directly
    let random_data = random_bytes(length);
    println!("2. Random bytes: {:?}", random_data);

    // Example 3: Generate a random alphanumeric string
    let charset =
        "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    let password = random_string(charset, length);
    println!("3. Random alphanumeric password: {}", password);

    // Example 4: Generate a random string with special characters
    let charset_with_special = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+[]{}|;:,.<>?";
    let secure_password = random_string(charset_with_special, length);
    println!("4. Random password with special chars: {}", secure_password);

    // Example 5: Generate a random hexadecimal string
    let hex_charset = "0123456789abcdef";
    let hex_string = random_string(hex_charset, length * 2); // Double length for hex representation
    println!("5. Random hex string: {}", hex_string);
}