Crate merkle_hash

Crate merkle_hash 

Source
Expand description

Crate merkle_hash makes it easy to find the hashes of all files and directories in a directory tree.

§Usage

To use this crate, add merkle_hash as a dependency to your project’s Cargo.toml:

[dependencies]
merkle_hash = "3.8"

§Features

  • Finds the master hash of a directory tree with ease.
  • Offers multiple hashing algorithms.
  • Allows including names in the hashing process.
  • Uses a merkle tree algorithm to compute the hashes of directories.
  • External iteration over the paths and hashes of files and directories.

§Limitations

  • Currently only supports UTF-8 paths and will fail if a path is not UTF-8 encoded.

§Optional

  • sha - Add this cargo feature to include SHA-256 and SHA-512 as hashing algorithms.
  • parallel - Enabled by default, this feature makes the crate utilize all available threads.
  • camino - Enabled by default, this feature makes all paths UTF-8 validated.
  • encode - Enabled by default, this feature adds the bytes_to_hex and to_hex_string functions.
  • retain - Disabled by default, this feature duplicates the children paths of directories upon traversal.
  • bincode - Disabled by default, this feature enables bincode support.

§Example: Get the master hash of a directory tree:

use merkle_hash::{Algorithm, MerkleTree};

let tree = MerkleTree::builder("/path/to/directory")
    .algorithm(Algorithm::Blake3)
    .hash_names(false)
    .build()?;
let master_hash = tree.root.item.hash;

§Example: Iterate over a directory tree, getting the hash of each file and directory:

use merkle_hash::{Encodable, MerkleTree};

let tree = MerkleTree::builder("/path/to/directory").build()?;
for item in tree {
    println!("{}: {}", item.path.relative, item.hash.to_hex_string());
}

§Example: Collapse the tree into any linear collection:

use std::collections::BTreeSet;
use merkle_hash::{MerkleItem, MerkleTree};

let tree = MerkleTree::builder("/path/to/directory").build()?;
let btree_set: BTreeSet<MerkleItem> = tree.into_iter().collect();

Re-exports§

pub use blake3;
pub use camino;
pub use rayon;

Modules§

error
Different types of errors for this crate

Structs§

MerkleItem
Holds the path, hash and children paths of a file or directory
MerkleNode
Represents a single node on the merkle tree
MerkleNodeIntoIter
Owned node iterator
MerkleNodeIter
Node iterator
MerklePath
A utility struct that contains an absolute path and a relative path
MerkleTree
Represents an indexed directory tree
MerkleTreeBuilder
Utility builder pattern

Enums§

Algorithm
Hashing algorithms to choose from

Traits§

Encodable
Utility trait for converting hashes to hex strings

Functions§

bytes_to_hex
Converts a hash to a hex string