summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2025-06-23 08:11:26 +0000
committerSanto Cariotti <santo@dcariotti.me>2025-06-23 08:11:26 +0000
commit3117b2fc4ff9cb3609f2c8ad4219df2ca37a23f7 (patch)
treec188419a796b3f0176f23f3cadb2070fcde54e36 /examples
parent9ec408203a1a13d2b8e450f7601b994070f0c91d (diff)
Add example which returns the root hash of a MT using Blake3
Diffstat (limited to 'examples')
-rw-r--r--examples/merkletree_blake3.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/examples/merkletree_blake3.rs b/examples/merkletree_blake3.rs
new file mode 100644
index 0000000..6b2adc0
--- /dev/null
+++ b/examples/merkletree_blake3.rs
@@ -0,0 +1,28 @@
+use mt_rs::{hasher::Blake3Hasher, merkletree::MerkleTree};
+
+fn main() {
+ // Collect filenames from command line arguments
+ let filenames: Vec<String> = std::env::args().skip(1).collect();
+
+ if filenames.is_empty() {
+ eprintln!("Usage: cargo run --exmaple merkletree_blake3 -- <file1> <file2> ...");
+ std::process::exit(1);
+ }
+
+ // Read file contents into a vector of bytes
+ let mut file_contents = Vec::new();
+ for filename in &filenames {
+ match std::fs::read(filename) {
+ Ok(contents) => file_contents.push(contents),
+ Err(e) => {
+ eprintln!("Failed to read file '{}': {}", filename, e);
+ std::process::exit(1);
+ }
+ }
+ }
+
+ let hasher = Blake3Hasher::new();
+ let tree = MerkleTree::new(hasher.clone(), file_contents.clone());
+
+ println!("{}", tree.root().hash());
+}