use base64;
use rand::rngs::EntropyRng;
use rand::RngCore;
use {Error, ErrorKind};
pub fn generate_random_bytes(len: u32) -> Result<Vec<u8>, Error> {
let mut rng = EntropyRng::new();
let mut bytes = vec![0u8; len as usize];
rng.try_fill_bytes(&mut bytes)
.map_err(|e| Error::new(ErrorKind::OsRngError).add_context(format!("{}", e)))?;
Ok(bytes)
}
pub fn generate_random_base64_encoded_string(len: u32) -> Result<String, Error> {
let mut rng = EntropyRng::new();
let mut bytes = vec![0u8; len as usize];
rng.try_fill_bytes(&mut bytes)
.map_err(|e| Error::new(ErrorKind::OsRngError).add_context(format!("{}", e)))?;
let output = base64::encode_config(&bytes, base64::STANDARD);
Ok(output)
}
pub fn generate_random_base64_encoded_string_config(
len: u32,
config: base64::Config,
) -> Result<String, Error> {
let mut rng = EntropyRng::new();
let mut bytes = vec![0u8; len as usize];
rng.try_fill_bytes(&mut bytes)
.map_err(|e| Error::new(ErrorKind::OsRngError).add_context(format!("{}", e)))?;
let output = base64::encode_config(&bytes, config);
Ok(output)
}