Created
November 6, 2021 16:01
-
-
Save torkleyy/54298c9f6844cff5a2cb6b315aa39682 to your computer and use it in GitHub Desktop.
Get SHA-256 of a Rust type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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