Currently I'm using the following code to get sha256 hashes of Rust structs and enums.
pub fn sha256<T: Sized + Serialize>(ser: T) -> [u8; 32] {
let str = ron::ser::to_string(&ser).expect("serialization has failed");
let mut hasher = Sha256::new();
hasher.update(str);
let hash = hasher.finalize();
*hash.as_ref()
}
This works, but is far from ideal:
There is a .hash()
method on many types, but that seems to be for 64-bit non-crypto hashing (HashMap, etc.).
How can I cryptographically-hash arbitrary Rust structs and enums, such that the hashes will be identical regardless of architecture/word-size/endianess? (I do not use usize
in these.)
If you want to hash an object with a cryptographic hash, you must necessarily turn it into a stream of bytes, since that's the only thing that cryptographic hashes accept. We would generally call this serialization.
There are some things you can do:
Alternatively, you could try building a custom Hasher
implementation which can also output a SHA-256 value. Your structure would then need to implement Hash
instead of Serialize
and you'd do the hashing incrementally. This might or might not be faster.