Skip to content
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

Feature request: Add a method to generate a collection of random bytes #209

Open
rmg-x opened this issue Dec 21, 2024 · 0 comments
Open

Comments

@rmg-x
Copy link

rmg-x commented Dec 21, 2024

I didn't see an obvious way to generate a collection of random bytes so I wrote a quick method to do so in rng.rs:

/// Generates a random collection of bytes. Advances the internal state of the RNG.
pub fn random_bytes<const N: usize>(&mut self) -> Result<[u8; N]> {
    let num_bytes: u32 = N
        .try_into()
        .expect("Failed while trying to convert usize to u32");

    let mut buf = [0u8; N];
    match unsafe {
        wolfssl_sys::wc_RNG_GenerateBlock(&mut self.0 as *mut _, buf.as_mut_ptr(), num_bytes)
    } {
        0 => Ok(buf),
        e => Err(Error::fatal(e)),
    }
}

and used it like this which worked fine:

use wolfssl::Random;

fn main() {
    let mut rand = Random::new().unwrap();
    let bytes = rand.random_bytes::<32>().unwrap();
    println!("{bytes:?}");
}

Would the maintainers consider adding a method like this to rng?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant