1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
extern crate rand;
use self::rand::{thread_rng, RngCore};
use hmac::Hmac;
use sha2::Digest;
const PBKDF2_ROUNDS: u32 = 2048;
const PBKDF2_BYTES: usize = 64;
pub(crate) fn sha256_first_byte(input: &[u8]) -> u8 {
sha2::Sha256::digest(input)[0]
}
pub(crate) fn gen_random_bytes(byte_length: usize) -> Vec<u8> {
let mut rng = thread_rng();
let mut bytes = vec![0u8; byte_length];
rng.fill_bytes(&mut bytes);
bytes
}
pub(crate) fn pbkdf2(input: &[u8], salt: &str) -> Vec<u8> {
let mut seed = vec![0u8; PBKDF2_BYTES];
pbkdf2::pbkdf2::<Hmac<sha2::Sha512>>(input, salt.as_bytes(), PBKDF2_ROUNDS, &mut seed);
seed
}