Skip to content

Instantly share code, notes, and snippets.

@torkleyy
Created November 6, 2021 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torkleyy/54298c9f6844cff5a2cb6b315aa39682 to your computer and use it in GitHub Desktop.
Save torkleyy/54298c9f6844cff5a2cb6b315aa39682 to your computer and use it in GitHub Desktop.
Get SHA-256 of a Rust type
// Following crates are needed:
// sha2, serde, bincode
use serde::Serialize;
#[derive(Serialize)]
struct MyType {
// some fields
}
impl MyType {
pub fn to_sha256(&self) -> [u8; 32] {
let mut hasher = sha2::Sha256::new();
bincode::serialize_into(&mut hasher, self).unwrap();
hasher.finalize().into()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment