passphrasex_common 0.3.0

Common code for PassPhraseX
Documentation
use rand::distributions::{DistString, Distribution};
use rand::Rng;
pub struct PasswordDist;

impl Distribution<u8> for PasswordDist {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u8 {
        const RANGE: u32 = 26 + 26 + 10 + 2;
        const GEN_ASCII_STR_CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
                abcdefghijklmnopqrstuvwxyz\
                0123456789\
                @-";

        loop {
            let var = rng.next_u32() >> (32 - 6);
            if var < RANGE {
                return GEN_ASCII_STR_CHARSET[var as usize];
            }
        }
    }
}

impl DistString for PasswordDist {
    fn append_string<R: Rng + ?Sized>(&self, rng: &mut R, string: &mut String, len: usize) {
        string.extend(self.sample_iter(rng).take(len).map(|c| c as char));
    }
}