1use cid::Cid;
6use multihash::Multihash;
7use multihash_codetable::{Code, MultihashDigest};
8use sha2::{Digest, Sha256};
9
10pub const DAG_CBOR_CODEC: u64 = 0x71;
12
13pub type ContentHash = [u8; 32];
15
16pub type DagCborMultihash = Multihash<64>;
18
19pub fn sha2_256_content_hash(bytes: &[u8]) -> ContentHash {
21 Sha256::digest(bytes).into()
22}
23
24pub fn dag_cbor_multihash(bytes: &[u8]) -> DagCborMultihash {
26 Code::Sha2_256.digest(bytes)
27}
28
29pub fn compute_cid_dag_cbor(bytes: &[u8]) -> String {
31 let hash = dag_cbor_multihash(bytes);
32 let cid = Cid::new_v1(DAG_CBOR_CODEC, hash);
33 cid.to_string().to_lowercase()
34}
35
36pub fn verify_dag_cbor_cid(cid_str: &str, bytes: &[u8]) -> (bool, String, String) {
41 let expected = compute_cid_dag_cbor(bytes);
42 let actual = cid_str.to_lowercase();
43 (expected == actual, expected, actual)
44}
45
46pub fn is_valid_cid_string(s: &str) -> bool {
48 Cid::try_from(s).is_ok()
49}
50
51pub fn try_parse_cid(s: &str) -> Option<Cid> {
53 Cid::try_from(s).ok()
54}