Merkle Tree Generator Online | Build & Verify Proofs (SHA-256/Keccak)
Hash Functions How It Works
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
| Algorithm | Output Size | Used by | Best for |
|---|---|---|---|
| SHA-256 | 256-bit (32 bytes) | Bitcoin, TLS, general-purpose | Standard Merkle trees, blockchain |
| SHA-512 | 512-bit (64 bytes) | High-security systems | Larger digest, collision resistance |
| Keccak-256 | 256-bit (32 bytes) | Ethereum, Solidity | EVM 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
Security Notes
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
- Wikipedia — Merkle Tree
- RFC 6962 — Certificate Transparency
- OpenZeppelin MerkleProof Library
- Bitcoin Developer Guide — Merkle Trees
Related Concepts
- SHA Hash Generator — Single Algorithm Hashing
- Digital Signature — Verify Data Authenticity
- Hash Compare — Multi-Algorithm Comparison
- Merkle Tree Generator
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
SHA Hash Generator
Generate SHA-256 or SHA-512 hashes from text or files online. Real-time computation, batch upload with CSV export, Hex/Base64 output.
Hash Compare
Compute and compare SHA-1, SHA-256, SHA-384, and SHA-512 hashes side by side online. Hex/Base64 output for checksum verification.
HMAC Generator
Generate HMAC (Hash-based Message Authentication Code) online with SHA-256/384/512. UTF-8/Hex/Base64 input and output, real-time computation.
Bcrypt Hash Generator
Generate and verify bcrypt password hashes online. Adjustable cost factor (4-20), real bcryptjs implementation, Node.js compatible.