iop_morpheus_proto/crypto/
hash.rs1use super::*;
2
3pub fn hash_bytes(content: &[u8]) -> String {
6 format!("cb{}", default_hasher(content))
7}
8
9pub type ContentId = String;
10
11pub trait Content: Serialize + Clone + Sized {
12 fn content_id(&self) -> Result<ContentId> {
13 digest_data(self)
14 }
15
16 fn validate_id(&self, content_id: impl Deref<Target = ContentId>) -> Result<bool> {
17 let calculated_hash = self.content_id()?;
18 Ok(calculated_hash == *content_id)
19 }
20}
21
22impl Content for serde_json::Value {}
23
24impl Content for Box<[u8]> {
25 fn content_id(&self) -> Result<ContentId> {
26 Ok(hash_bytes(self.as_ref()))
27 }
28}
29
30impl Content for Vec<u8> {
31 fn content_id(&self) -> Result<ContentId> {
32 Ok(hash_bytes(self.as_ref()))
33 }
34}