#hash #directory #file #checksum

merkle_hash

Finds the hashes of all files and directories in a directory tree

18 stable releases

3.8.0 Mar 30, 2025
3.7.0 Aug 2, 2024
3.6.1 Jan 27, 2024
3.5.0 Mar 24, 2023
1.1.1 Mar 31, 2022

#84 in Algorithms

Download history 1233/week @ 2025-02-02 1005/week @ 2025-02-09 1287/week @ 2025-02-16 1188/week @ 2025-02-23 1638/week @ 2025-03-02 1398/week @ 2025-03-09 1237/week @ 2025-03-16 945/week @ 2025-03-23 1189/week @ 2025-03-30 1135/week @ 2025-04-06 724/week @ 2025-04-13 976/week @ 2025-04-20 965/week @ 2025-04-27 936/week @ 2025-05-04 885/week @ 2025-05-11 907/week @ 2025-05-18

3,797 downloads per month
Used in 23 crates (12 directly)

MIT license

31KB
574 lines

merkle_hash

Finds 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.

Examples

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;

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());
}

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();

Release notes for 3.8

Versioning

  • Any major version of this crate may contain changes to the hashing algorithm.
  • Any minor version of this crate may contain breaking changes to the API.
  • Any patch version of this crate will only contain bug fixes and no breaking changes.

Used technologies

  • rayon for multithreaded directory reading and hashing.
  • camino to ensure that paths are always utf-8.
  • blake3 for the blake3 hashing of file contents.
  • sha2 for the sha256 and sha512 hashing of file contents.
  • bincode for bincode serialization / deserialization.

License

Licensed under MIT license.

Dependencies

~1.6–2.5MB
~60K SLoC