Function spinoso_securerandom::alphanumeric

source ·
pub fn alphanumeric(len: Option<i64>) -> Result<Vec<u8>, Error>
Expand description

Generate a random sequence of ASCII alphanumeric bytes.

If len is Some and non-negative, generate a String of len random ASCII alphanumeric bytes. If len is None, generate 16 random alphanumeric bytes.

The returned Vec<u8> is guaranteed to contain only ASCII bytes.

§Examples

let bytes = spinoso_securerandom::alphanumeric(Some(1024))?;
let bytes = String::from_utf8(bytes)?;
assert_eq!(bytes.len(), 1024);
assert!(bytes.is_ascii());
assert!(bytes.find(|ch: char| !ch.is_ascii_alphanumeric()).is_none());

§Errors

If the given length is negative, return an ArgumentError.

If an allocation error occurs, an error is returned.