1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
use crypto::mac::Mac;
use std::convert::TryInto;

pub fn nonce() -> [u8; 32] {
    rand::random()
}

pub fn compute_challenge(private_key: &[u8], nonce: &[u8]) -> [u8; 32] {
    let mut hasher = crypto::hmac::Hmac::new(crypto::sha2::Sha256::new(), private_key);
    hasher.input(nonce);
    let slice = hasher.result().code().to_vec().into_boxed_slice();
    let array: Box<[u8; 32]> = match slice.try_into() {
        Ok(array) => array,
        Err(_) => unreachable!(),
    };
    *array
}