Created
February 1, 2016 21:02
-
-
Save gavofyork/013c4ab0ec3cc057f3cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub enum Alphabet { | |
All, | |
Low, | |
Mid, | |
Custom(Bytes), | |
} | |
pub struct StandardMap { | |
alphabet: Alphabet, | |
min_key: usize, | |
journal_key: usize, | |
count: usize, | |
} | |
impl StandardMap { | |
fn random_bytes(min_count: usize, journal_count: usize, seed: &mut H256) -> Vec<u8> { | |
assert!(min_count + journal_count <= 32); | |
*seed = seed.sha3(); | |
let r = min_count + (seed.bytes()[31] as usize % (journal_count + 1)); | |
seed.bytes()[0..r].to_vec() | |
} | |
fn random_value(seed: &mut H256) -> Bytes { | |
*seed = seed.sha3(); | |
match seed.bytes()[0] % 2 { | |
1 => vec![seed.bytes()[31];1], | |
_ => seed.bytes().to_vec(), | |
} | |
} | |
fn random_word(alphabet: &[u8], min_count: usize, journal_count: usize, seed: &mut H256) -> Vec<u8> { | |
assert!(min_count + journal_count <= 32); | |
*seed = seed.sha3(); | |
let r = min_count + (seed.bytes()[31] as usize % (journal_count + 1)); | |
let mut ret: Vec<u8> = Vec::with_capacity(r); | |
for i in 0..r { | |
ret.push(alphabet[seed.bytes()[i] as usize % alphabet.len()]); | |
} | |
ret | |
} | |
pub fn make(&self) -> Vec<(Bytes, Bytes)> { | |
let low = b"abcdef"; | |
let mid = b"@QWERTYUIOPASDFGHJKLZXCVBNM[/]^_"; | |
let mut d: Vec<(Bytes, Bytes)> = Vec::new(); | |
let mut seed = H256::new(); | |
for _ in 0..self.count { | |
let k = match self.alphabet { | |
Alphabet::All => Self::random_bytes(self.min_key, self.journal_key, &mut seed), | |
Alphabet::Low => Self::random_word(low, self.min_key, self.journal_key, &mut seed), | |
Alphabet::Mid => Self::random_word(mid, self.min_key, self.journal_key, &mut seed), | |
Alphabet::Custom(ref a) => Self::random_word(&a, self.min_key, self.journal_key, &mut seed), | |
}; | |
let v = Self::random_value(&mut seed); | |
d.push((k, v)) | |
} | |
d | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment