evobench_tools/utillib/crypto_hash.rs
1//! Cryptographically hash a data structure
2
3// `bincode` has its own introspection macro. And there's not even a
4// widely used serde+bincode crate? So, go with serde_json.
5
6use base64::Engine;
7use serde::Serialize;
8use sha2::Digest;
9
10/// Calculate a base64-encoded (in URL-safe variant, i.e. no '/' or
11/// '+') hash value
12pub fn crypto_hash<T: Serialize>(val: &T) -> String {
13 // When do serializers to string ever give errors?
14 let s = serde_json::to_string(val).expect("can't fail we hope?");
15 let mut hasher = sha2::Sha256::new();
16 hasher.update(&s);
17 base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(hasher.finalize())
18}