首页 > 区块链 > 使用Java语言编写你的第一个区块链程序
区块猴  

使用Java语言编写你的第一个区块链程序

摘要:区块链程序 近期火热的加密货币的每个块中都包含了所有交易的集合签名,这个签名就是用Merkle tree实现的,Merkle树用于产生整个事务集合的整体数字指纹,提供非常有效的过程来验证事务是否包括在块中。 下面我们来看一下用java代码实现 Merkle tree的过程。 哈希树的跟节点称为Mer

近期火热的加密货币的每个块中都包含了所有交易的集合签名,这个签名就是用Merkle tree实现的,Merkle树用于产生整个事务集合的整体数字指纹,提供非常有效的过程来验证事务是否包括在块中。

下面我们来看一下用java代码实现 Merkle tree的过程。

哈希树的跟节点称为Merkle根,Merkle树可以仅用log2(N)的时间复杂度检查任何一个数据元素是否包含在树中:

package test;

import java.security.MessageDigest;

import java.util.ArrayList;

import java.util.List;

public class MerkleTrees {

// transaction List

List txList;

// Merkle Root

String root;

/**

* constructor

* @param txList transaction List 交易List

*/

public MerkleTrees(List txList) {

this.txList = txList;

root = "";

}

/**

* execute merkle_tree and set root.

*/

public void merkle_tree() {

List tempTxList = new ArrayList();

for (int i = 0; i < this.txList.size(); i++) {

tempTxList.add(this.txList.get(i));

}

List newTxList = getNewTxList(tempTxList);

while (newTxList.size() != 1) {

newTxList = getNewTxList(newTxList);

}

this.root = newTxList.get(0);

}

/**

* return Node Hash List.

* @param tempTxList

* @return

*/

private List getNewTxList(List tempTxList) {

List newTxList = new ArrayList();

int index = 0;

while (index < tempTxList.size()) {

// left

String left = tempTxList.get(index);

index++;

// right

String right = "";

if (index != tempTxList.size()) {

right = tempTxList.get(index);

}

// sha2 hex value

String sha2HexValue = getSHA2HexValue(left + right);

newTxList.add(sha2HexValue);

index++;

}

return newTxList;

}

/**

* Return hex string

* @param str

* @return

*/

public String getSHA2HexValue(String str) {

byte[] cipher_byte;

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

* @return

*/

public String getRoot() {

return this.root;

}

}

我们将交易的数据,放入到List中:

List tempTxList = new ArrayList();

tempTxList.add("a");

tempTxList.add("b");

tempTxList.add("c");

tempTxList.add("d");

tempTxList.add("e");

准备交易数据

计算出每个数据的hash值,从左到右逐步组成树的左右节点

执行循环知道最后只剩下一个数据

private List getNewTxList(List tempTxList) {

List newTxList = new ArrayList();

int index = 0;

while (index < tempTxList.size()) {

// left

String left = tempTxList.get(index);

index++;

// right

String right = "";

if (index != tempTxList.size()) {

right = tempTxList.get(index);

}

// sha2 hex value

String sha2HexValue = getSHA2HexValue(left + right);

newTxList.add(sha2HexValue);

index++;

}

下面我们进行一个简单的测试

package test;

import java.util.ArrayList;

import java.util.List;

public class App {

public static void main(String [] args) {

List tempTxList = new ArrayList();

tempTxList.add("a");

tempTxList.add("b");

tempTxList.add("c");

tempTxList.add("d");

tempTxList.add("e");

MerkleTrees merkleTrees = new MerkleTrees(tempTxList);

merkleTrees.merkle_tree();

System.out.println("root : " + merkleTrees.getRoot());

}

}

使用Java语言编写你的第一个区块链程序

免责声明
世链财经作为开放的信息发布平台,所有资讯仅代表作者个人观点,与世链财经无关。如文章、图片、音频或视频出现侵权、违规及其他不当言论,请提供相关材料,发送到:2785592653@qq.com。
风险提示:本站所提供的资讯不代表任何投资暗示。投资有风险,入市须谨慎。
世链粉丝群:提供最新热点新闻,空投糖果、红包等福利,微信:msy2134。