Skip to content

giorgeabdala/merkleroot-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Merkle Root Implementation in Rust

A simple implementation of Merkle tree root calculation in Rust using SHA256 hashing.

Features

  • 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

Functions

hash_data(data: &[u8]) -> String

Computes SHA256 hash of input data and returns it as a hexadecimal string.

calculate_merkle_root(transactions: &[String]) -> String

Calculates the Merkle root from a list of transactions by:

  1. Hashing each transaction to create leaf nodes
  2. Building the tree bottom-up by pairing and hashing nodes
  3. Handling odd numbers by duplicating the last hash
  4. Returning the final root hash

Usage

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);

Running Tests

cargo test

Dependencies

  • sha2 - SHA256 hashing implementation

License

This project is open source and available under the MIT License.

About

A simple implementation of Merkle tree root calculation in Rust using SHA256 hashing.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages