I have used this library to generate a Merkle tree for me.
It uses default SHA256 algorithm to hash a block and then combines the hash of the leaves, which it hashes again to form a root(node) of the Merkle tree.
I find out the hash of the leaves by the following:
use merkle_tree::{MerkleTree};
use rustc_serialize::hex::ToHex;
fn main() {
let block = "foo";
let merkle_tree: MerkleTree = MerkleTree::build(&[block, block]);
let leaves = merkle_tree.leaves();
let leaves_to_hex = [29, 32, 57, 250, 121, 113, 244, 191, 1, 161, 194, 12, 178,
163, 254, 122, 244, 104, 101, 202, 156, 217, 184, 64, 194, 6, 61, 248, 254, 196, 255, 117].to_hex();
println!("{:?}", leaves);
// Outputs [29, 32, 57, 250, 121, 113, 244, 191, 1, 161, 194, 12, 178,
// 163, 254, 122, 244, 104, 101, 202, 156, 217, 184, 64, 194, 6, 61, 248, 254, 196, 255, 117]
println!("{:?}", leaves_to_hex);
// Outputs 1d2039fa7971f4bf01a1c20cb2a3fe7af46865ca9cd9b840c2063df8fec4ff75
}
It basically returns as_bytes
array on which I use to_hex
to obtain the hash.
When I match this hash with an SHA256 online, it doesn't match. What's not correct?
My Cargo.toml
:
[dependencies]
merkle_tree = {git = "https://github.com/melekes/merkle-tree-rs"}
sha2 = "0.10.6"
rustc-serialize = "0.3"
hex-literal = "0.3.4"
Not all dependencies maybe used in this very snippet of code, they are from the project where they are used somewhere.