A simple implementation of Merkle tree root calculation in Rust using SHA256 hashing.
- SHA256 Hashing: Uses the SHA256 algorithm to hash transaction data
- Merkle Tree Construction: Builds a binary tree bottom-up from transaction hashes
- Odd Number Handling: Automatically duplicates the last hash when dealing with odd numbers of transactions
- Comprehensive Tests: Full test suite covering various scenarios
Computes SHA256 hash of input data and returns it as a hexadecimal string.
Calculates the Merkle root from a list of transactions by:
- Hashing each transaction to create leaf nodes
- Building the tree bottom-up by pairing and hashing nodes
- Handling odd numbers by duplicating the last hash
- Returning the final root hash
let transactions = vec![
"tx1".to_string(),
"tx2".to_string(),
"tx3".to_string(),
"tx4".to_string()
];
let merkle_root = calculate_merkle_root(&transactions);
println!("Merkle Root: {}", merkle_root);cargo testsha2- SHA256 hashing implementation
This project is open source and available under the MIT License.