[−][src]Function mkpasswd::generate_with_rng
pub fn generate_with_rng<R>(
alphabet: &[u8],
length: usize,
rng: &mut R
) -> Result<Vec<u8>, Error> where
R: RngCore + CryptoRng,
Generates a length
long password made of bytes from alphabet
using random data provided by rng
.
If the input alphabet only contains valid UTF-8 bytes, this function is guaranteed to only return valid UTF-8 as well.
Example
This example is using a custom alphabet as well.
use mkpasswd::generate_with_rng; use rand_os::OsRng; let alphabet = b"qwerty+asdfgh-zxcvbn"; let mut rng = OsRng::new().unwrap(); let password = generate_with_rng(alphabet, 16, &mut rng).unwrap(); assert_eq!(password.len(), 16);
It is recommended to sanitize the alphabet before, removing duplicate values:
let mut my_alphabet = b"Hello world!".to_vec(); my_alphabet.sort(); my_alphabet.dedup(); // 'l' and 'o' is only once in the buffer now assert_eq!(my_alphabet, b" !Hdelorw");
Errors
This functions returns an error if rng
fails to generate random data.
If alphabet.len()
is 0
, this functions panics.