Question

Write down a Java program that calculates the Merkle root for the following dataset: 7246 9716...

Write down a Java program that calculates the Merkle root for the following dataset:

7246

9716

2017

6329

2831

1171

5690

7739

9463

8363

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java Program to Calculate Merkel Root from a dataset:

Merkel Root : Merkel Root is the final created hash from the input dataset, it is a Blockchain technology and is used to summarize all the transactions in a block. Currently used with Bitcoin it  produces overall digital fingerprint of the entire set of transactions, and provide a very efficient process for verifying if a transaction is included in a block or not.

/*Program starts here*/

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

public class MerkleTrees {

// Dataset list for capturing the data inputs
List<String> txnList;
// Merkle Root Variable ( String Type )
String root;
  
//Constructor
public MerkleTrees(List<String> txnList) {
this.txnList = txnList;
root = "";
}

//Merkle_tree execution with void return type used to set root.
public void merkle_tree() {
  
List<String> tempTxnList = new ArrayList<String>();
  

//Loop for capturing the dataset
for (int i = 0; i < this.txnList.size(); i++) {
tempTxnList.add(this.txnList.get(i));
}
  
List<String> newTxnList = getNewTxnList(tempTxnList);
while (newTxnList.size() != 1) {
newTxnList = getNewTxnList(newTxnList);
}
  
this.root = newTxnList.get(0);
}
  
/Return Node Hash List, Private access specifier.
private List<String> getNewTxnList(List<String> tempTxnList) {
  
List<String> newTxnList = new ArrayList<String>();
int index = 0;
while (index < tempTxnList.size()) {
// left
String left = tempTxnList.get(index);
index++;

// right
String right = "";
if (index != tempTxnList.size()) {
right = tempTxnList.get(index);
}

// sha2 hex value
String sha2HexValue = getSHA2HexValue(left + right);
newTxnList.add(sha2HexValue);
index++;
  
}
  
return newTxnList;
}
  
//Returning hex string
public String getSHA2HexValue(String str) {
byte[] cipher_byte;

//Exception handeling with Try block
try{
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(str.getBytes());
cipher_byte = md.digest();
StringBuilder sb = new StringBuilder(2 * cipher_byte.length);
for(byte b: cipher_byte) {
sb.append(String.format("%02x", b&0xff) );
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
  
return "";
}
  
//Get Root, String return type
public String getRoot() {
return this.root;
}
  
}

/*Program ends here*/

Add a comment
Answer #2

Please find the three classes and output file.

Add a comment
Know the answer?
Add Answer to:
Write down a Java program that calculates the Merkle root for the following dataset: 7246 9716...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT