#hash #checksum #file #directory #tree

merkle_hash

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

16 stable releases

3.6.1 Jan 27, 2024
3.5.0 Mar 24, 2023
3.2.0 Feb 27, 2023
3.1.0 Jan 31, 2023
1.1.1 Mar 31, 2022

#93 in Algorithms

Download history 78/week @ 2023-10-31 48/week @ 2023-11-07 30/week @ 2023-11-14 92/week @ 2023-11-21 131/week @ 2023-11-28 49/week @ 2023-12-05 82/week @ 2023-12-12 46/week @ 2023-12-19 144/week @ 2023-12-26 27/week @ 2024-01-02 48/week @ 2024-01-09 60/week @ 2024-01-16 116/week @ 2024-01-23 130/week @ 2024-01-30 134/week @ 2024-02-06 154/week @ 2024-02-13

547 downloads per month
Used in 6 crates

MIT license

25KB
469 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.6"

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

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

  • The duplication of children paths for directories is now hidden behind the retain feature.
  • The functions for encoding a hash into a hexadecimal string have now been hidden behind the encode feature.

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.
  • anyhow to ease-out the handling of errors.
  • blake3 for the blake3 hashing of file contents.
  • sha2 for the sha256 and sha512 hashing of file contents.

License

Licensed under MIT license.

Dependencies

~1.5–2.1MB
~50K SLoC