use rand::{rngs::OsRng, Rng};
pub fn get_secure_random_bytes(size: usize) -> Vec<u8> {
let mut rng = OsRng; let mut bytes = vec![0u8; size];
rng.fill(&mut bytes[..]); bytes
}
pub fn get_secure_random_words(size: usize) -> Vec<u16> {
let mut rng = OsRng; let mut words = vec![0u16; size];
rng.fill(&mut words[..]); words
}
#[test]
fn test_get_secure_random_bytes() {
use std::process::Command;
use std::env;
let byte_length = 32;
let rust_random_bytes = get_secure_random_bytes(byte_length);
let mut ts_script_path = env::current_dir().expect("Failed to get current directory");
ts_script_path.push("src/ts_lib/get_secure_random.ts");
println!("Using TypeScript script path: {:?}", ts_script_path);
let output = Command::new("npx")
.arg("tsx")
.arg(ts_script_path)
.arg(byte_length.to_string())
.output()
.expect("Failed to execute TypeScript script");
assert!(
output.status.success(),
"TypeScript script failed: {}",
String::from_utf8_lossy(&output.stderr)
);
let ts_random_bytes_hex = String::from_utf8_lossy(&output.stdout).trim().to_string();
let ts_random_bytes = hex::decode(&ts_random_bytes_hex).expect("Invalid hex output from TypeScript script");
assert_eq!(
rust_random_bytes.len(),
ts_random_bytes.len(),
"Length of random bytes does not match"
);
println!("Rust Random Bytes: {:?}", rust_random_bytes);
println!("TS Random Bytes: {:?}", ts_random_bytes);
}
#[test]
fn test_get_secure_random_words() {
let word_length = 16;
let rust_random_words = get_secure_random_words(word_length);
assert_eq!(
rust_random_words.len(),
word_length,
"Length of random words does not match"
);
println!("Rust Random Words: {:?}", rust_random_words);
}