2018-03-30 10:43:38 -07:00
|
|
|
//! The `hash` module provides functions for creating SHA-256 hashes.
|
2018-03-29 12:20:54 -06:00
|
|
|
|
2018-05-11 13:06:05 -06:00
|
|
|
use generic_array::typenum::U32;
|
2018-05-29 21:20:28 -06:00
|
|
|
use generic_array::GenericArray;
|
2018-03-06 17:20:37 -07:00
|
|
|
use sha2::{Digest, Sha256};
|
|
|
|
|
2018-03-06 17:36:45 -07:00
|
|
|
pub type Hash = GenericArray<u8, U32>;
|
2018-03-06 17:20:37 -07:00
|
|
|
|
|
|
|
/// Return a Sha256 hash for the given data.
|
2018-03-06 17:36:45 -07:00
|
|
|
pub fn hash(val: &[u8]) -> Hash {
|
2018-03-06 17:20:37 -07:00
|
|
|
let mut hasher = Sha256::default();
|
|
|
|
hasher.input(val);
|
2018-06-18 17:28:28 -07:00
|
|
|
|
|
|
|
// At the time of this writing, the sha2 library is stuck on an old version
|
|
|
|
// of generic_array (0.9.0). Decouple ourselves with a clone to our version.
|
|
|
|
GenericArray::clone_from_slice(hasher.result().as_slice())
|
2018-03-06 17:20:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the hash of the given hash extended with the given value.
|
2018-03-06 17:36:45 -07:00
|
|
|
pub fn extend_and_hash(id: &Hash, val: &[u8]) -> Hash {
|
2018-03-06 17:20:37 -07:00
|
|
|
let mut hash_data = id.to_vec();
|
|
|
|
hash_data.extend_from_slice(val);
|
|
|
|
hash(&hash_data)
|
|
|
|
}
|