Merkle Tree Generator Online | Build & Verify Proofs (SHA-256/Keccak)

Hash Functions
100% client-side. Processed locally in your browser using the Web Crypto API. Nothing is sent to any server.

How It Works

Merkle roots are only comparable when all conventions match. Hash function, leaf encoding (UTF-8 vs hex), odd-leaf rule (duplicate vs promote), and concatenation mode (raw vs sorted-pair) must all match between tree builder and verifier. A mismatch in any one produces a completely different root.

What Is a Merkle Tree?

A Merkle tree (or hash tree) is a binary tree structure where every leaf node contains the hash of a data block, and every non-leaf node contains the hash of its two children. The root hash (Merkle root) serves as a compact fingerprint of the entire dataset. Merkle trees enable efficient verification that a specific data item belongs to a large set without downloading the entire set — the proof size is O(log N).

When to Use Merkle Trees

  • Blockchain — Bitcoin, Ethereum, and other blockchains use Merkle trees to summarize all transactions in a block into a single root hash
  • Data integrity verification — prove that a specific file belongs to a large dataset without transferring all files
  • Smart contracts — Solidity uses Merkle proofs for airdrops, whitelists, and gas-efficient membership verification
  • Version control — Git uses Merkle trees (indirectly) to track file changes across commits
  • Certificate transparency — CT logs use Merkle trees to prove a certificate was included in the public log

Hash Algorithm Comparison

AlgorithmOutput SizeUsed byBest for
SHA-256256-bit (32 bytes)Bitcoin, TLS, general-purposeStandard Merkle trees, blockchain
SHA-512512-bit (64 bytes)High-security systemsLarger digest, collision resistance
Keccak-256256-bit (32 bytes)Ethereum, SolidityEVM compatibility, smart contracts

Standards Compliance

  • RFC 6962 — Certificate Transparency (uses SHA-256 Merkle trees)
  • Bitcoin protocol — uses SHA-256 with duplicate odd-leaf rule
  • Ethereum Yellow Paper — uses Keccak-256 Merkle-Patricia tries
  • OpenZeppelin MerkleProof — Solidity library using sorted-pair concatenation
  • NIST FIPS 180-4 — SHA-2 (SHA-256, SHA-512) specification

Last updated:

Code Examples

JavaScript (Browser — @noble/hashes)

// SHA-256 Merkle tree
import { sha256 } from '@noble/hashes/sha256';

// Hash leaves
const leaves = ['a', 'b', 'c', 'd'].map(
  x => sha256(new TextEncoder().encode(x))
);

// Build tree (duplicate odd-leaf rule)
let level = leaves;
while (level.length > 1) {
  const next = [];
  for (let i = 0; i < level.length; i += 2) {
    const left = level[i];
    const right = level[i + 1] || level[i];  // duplicate
    next.push(sha256(new Uint8Array([...left, ...right])));
  }
  level = next;
}
const root = level[0];
console.log('Merkle root:',
  Array.from(root).map(b => b.toString(16).padStart(2, '0')).join('')
);

Python

import hashlib

def merkle_root(leaves):
    if not leaves:
        return None
    level = [hashlib.sha256(l.encode()).digest() for l in leaves]
    while len(level) > 1:
        next_level = []
        for i in range(0, len(level), 2):
            left = level[i]
            right = level[i + 1] if i + 1 < len(level) else level[i]
            next_level.append(hashlib.sha256(left + right).digest())
        level = next_level
    return level[0].hex()

print(merkle_root(['a', 'b', 'c', 'd']))

Go

package main

import (
    "crypto/sha256"
    "encoding/hex"
    "fmt"
)

func merkleRoot(leaves []string) string {
    var level [][]byte
    for _, l := range leaves {
        h := sha256.Sum256([]byte(l))
        level = append(level, h[:])
    }
    for len(level) > 1 {
        var next [][]byte
        for i := 0; i < len(level); i += 2 {
            left := level[i]
            right := left
            if i+1 < len(level) {
                right = level[i+1]
            }
            combined := append(left, right...)
            h := sha256.Sum256(combined)
            next = append(next, h[:])
        }
        level = next
    }
    return hex.EncodeToString(level[0])
}

func main() {
    fmt.Println(merkleRoot([]string{"a", "b", "c", "d"}))
}

Rust

use sha2::{Sha256, Digest};

fn merkle_root(leaves: &[&str]) -> String {
    let mut level: Vec<Vec<u8>> = leaves.iter()
        .map(|l| Sha256::digest(l.as_bytes()).to_vec())
        .collect();

    while level.len() > 1 {
        let mut next = Vec::new();
        for chunk in level.chunks(2) {
            let left = &chunk[0];
            let right = if chunk.len() > 1 {
                &chunk[1]
            } else {
                left  // duplicate
            };
            let mut hasher = Sha256::new();
            hasher.update(left);
            hasher.update(right);
            next.push(hasher.finalize().to_vec());
        }
        level = next;
    }
    hex::encode(&level[0])
}

fn main() {
    println!("{}", merkle_root(&["a", "b", "c", "d"]));
}

Node.js

const crypto = require('crypto');

function merkleRoot(leaves) {
  let level = leaves.map(l =>
    crypto.createHash('sha256').update(l).digest()
  );

  while (level.length > 1) {
    const next = [];
    for (let i = 0; i < level.length; i += 2) {
      const left = level[i];
      const right = level[i + 1] || left;  // duplicate
      next.push(
        crypto.createHash('sha256')
          .update(Buffer.concat([left, right]))
          .digest()
      );
    }
    level = next;
  }
  return level[0].toString('hex');
}

console.log(merkleRoot(['a', 'b', 'c', 'd']));

Common Pitfalls

Mismatched conventions. Merkle roots are only comparable when all conventions match: hash function, leaf encoding, odd-leaf rule, and concatenation mode. A mismatch in any one produces a completely different root.
Using raw Keccak-256 instead of SHA3-256. Ethereum uses Keccak-256, which is NOT the same as NIST SHA3-256. They differ in the padding. Make sure you use the correct variant for your target system.
Not handling odd-leaf levels correctly. When a tree level has an odd number of nodes, the last node is unpaired. Bitcoin duplicates it (hashes with itself), while some systems promote it unchanged. Match the convention of your target system.
Confusing sorted-pair with raw concatenation. OpenZeppelin's MerkleProof library uses sorted-pair mode (smaller hash first), which eliminates the need to track left/right position. Standard Merkle trees use raw concatenation (left + right in order).
Not pre-hashing leaves. In most implementations, leaves are hashed before being added to the tree. If you feed raw data directly, your root will not match systems that pre-hash leaves.

Security Notes

Merkle proofs are logarithmic. For N leaves, proof size is O(log N), making them efficient for large datasets. A tree with 1 million leaves requires only ~20 proof steps.
Root is tamper-evident. Changing any single leaf produces a completely different root, making it easy to detect data tampering.
Sorted-pair simplifies verification. OpenZeppelin mode eliminates left/right position tracking in smart contracts, reducing gas costs and implementation complexity.
Use for membership proofs. Merkle trees enable proving that a specific item belongs to a set without revealing the entire set — fundamental for privacy-preserving protocols.

Disclaimer

This tool is provided for educational and development purposes. Merkle trees are used in critical systems including blockchains and certificate transparency logs — always verify conventions match your target system and consult domain-specific documentation. All computation occurs entirely in your browser.

Further Reading

Related Concepts

Related Tools

Need to generate a plain hash? Try the SHA Hash Generator. For HMAC generation with a secret key, see the HMAC Generator. You can also verify digital signatures with the Digital Signature & Verify tool.

Frequently Asked Questions

What is the difference between duplicate and promote odd-leaf rules?

When a tree level has an odd number of nodes, the last node is unpaired. "Duplicate" (Bitcoin convention) hashes the odd node with itself. "Promote" carries the odd node to the next level unchanged. Both produce valid trees but different roots — match the convention of the system you are verifying against.

What is sorted-pair (OpenZeppelin) concatenation?

In standard (raw) mode, left and right child hashes are concatenated in order before re-hashing. In sorted-pair mode (used by OpenZeppelin's MerkleProof library), the two hashes are compared byte-by-byte and the smaller one goes first. This eliminates the need to encode left/right position in proofs, simplifying smart contract verification.

Which hash algorithms are supported?

SHA-256 (Bitcoin standard), SHA-512, and Keccak-256 (Ethereum's original hash, distinct from NIST SHA3-256). All three produce 256-bit or 512-bit digests. Keccak-256 is used by Ethereum for Merkle-Patricia tries and Solidity's abi.encodePacked.

Why doesn't my root match another tool's root?

Merkle roots are only comparable when all conventions match: same hash function, same leaf encoding (UTF-8 vs hex), same odd-leaf rule (duplicate vs promote), and same concatenation mode (raw vs sorted). A mismatch in any one produces a completely different root.

How do I verify a leaf belongs to the tree?

Switch to Verify mode, paste the expected Merkle root, the leaf data, and the proof JSON (array of sibling hashes). The tool recomputes the root by hashing the leaf with each proof step, then compares it to your expected root. If they match, the leaf is provably part of the tree.

Privacy: This tool runs entirely in your browser — nothing is uploaded or stored. · Disclaimer: For educational purposes only. Not professional security advice.

More in Hash