hashtree_core/lib.rs
1//! HashTree - Simple content-addressed merkle tree storage
2//!
3//! Rust-first library for building merkle trees with content-hash addressing:
4//! SHA256(content) -> content
5//!
6//! # Overview
7//!
8//! HashTree provides a simple, efficient way to build and traverse content-addressed
9//! merkle trees. It uses SHA256 for hashing and MessagePack for tree node encoding.
10//!
11//! Content is CHK (Content Hash Key) encrypted by default, enabling deduplication
12//! even for encrypted content. Use `.public()` config to disable encryption.
13//!
14//! # Core Concepts
15//!
16//! - **Blobs**: Raw data stored directly by their hash (SHA256(data) -> data)
17//! - **Tree Nodes**: MessagePack-encoded nodes with links to children (SHA256(msgpack(node)) -> msgpack(node))
18//! - **Links**: References to child nodes with optional name and size metadata
19//! - **Cid**: Content identifier with hash + optional encryption key
20//!
21//! # Example
22//!
23//! ```rust
24//! use hashtree_core::{HashTree, HashTreeConfig, MemoryStore};
25//! use std::sync::Arc;
26//!
27//! #[tokio::main]
28//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
29//! let store = Arc::new(MemoryStore::new());
30//! let tree = HashTree::new(HashTreeConfig::new(store));
31//!
32//! // Store content (encrypted by default)
33//! let (cid, _size) = tree.put(b"Hello, World!").await?;
34//!
35//! // Read it back
36//! let data = tree.get(&cid).await?;
37//! assert_eq!(data, Some(b"Hello, World!".to_vec()));
38//!
39//! Ok(())
40//! }
41//! ```
42
43pub mod builder;
44pub mod codec;
45pub mod crypto;
46pub mod diff;
47pub mod hash;
48pub mod hashtree;
49pub mod nhash;
50pub mod reader;
51pub mod store;
52pub mod types;
53
54// Re-exports for convenience
55// Main API - unified HashTree
56pub use hashtree::{HashTree, HashTreeConfig, HashTreeError, verify_tree as hashtree_verify_tree};
57
58// Constants
59pub use builder::{BEP52_CHUNK_SIZE, DEFAULT_CHUNK_SIZE, DEFAULT_MAX_LINKS};
60
61// Low-level codec
62pub use codec::{
63 decode_tree_node, encode_and_hash, encode_tree_node, get_node_type, is_directory_node,
64 is_tree_node, try_decode_tree_node, CodecError,
65};
66pub use hash::{sha256, verify};
67
68// Reader types (used by HashTree)
69pub use reader::{verify_tree, ReaderError, TreeEntry, VerifyResult, WalkEntry};
70
71// Store
72pub use store::{MemoryStore, Store, StoreError};
73pub use types::{from_hex, hash_equals, to_hex, Cid, CidParseError, DirEntry, Hash, Link, LinkType, PutResult, TreeNode};
74pub use nhash::{
75 decode as nhash_or_nref_decode, is_nhash, is_nref, nhash_decode, nhash_encode,
76 nhash_encode_full, nref_decode, nref_encode, DecodeResult, NHashData, NHashError, NRefData,
77};
78
79pub use crypto::{
80 content_hash, could_be_encrypted, decrypt, decrypt_chk, encrypt, encrypt_chk, encrypted_size,
81 encrypted_size_chk, generate_key, key_from_hex, key_to_hex, plaintext_size, CryptoError,
82 EncryptionKey,
83};
84
85// Tree diff operations
86pub use diff::{collect_hashes, collect_hashes_with_progress, tree_diff, tree_diff_streaming, tree_diff_with_old_hashes, DiffStats, TreeDiff};