Merkle树的实现

 

简介

本篇文章是对Merkle tree的解释。Merkle tree是一种应用在比特币中的技术。本文的目标是通过代码来理解它的实现过程。

环境

Jdk 1.8.0_66

Idea

Merkle树

Merkle tree(哈希树)是一种数据结构,用于验证任何类型的数据存储、处理和传输。

目前,Merkle树的主要用途是确保在对等网络中从其他对等网络接收到的数据块未被损坏和修改,甚至检查其他对等网络是否存在并发送假块。

Merkle树应用范围:git, Amazon的Dynamo, Cassandra和 比特币

比特币区块链中的每一个区块都包含了区块中所有交易的摘要,使用的是Merkle树。

20142337_dSFR.png

Merkle树被用在比特币中,用来汇总一个区块的所有交易,生成整个交易集的整体数字指纹,提供一个非常有效的过程来验证一个交易是否被包含在一个块中。

重要的是检查是否包含块中的特定事务。为了这个,使用Merkle根。

Merkle树是通过递归的散列成对的节点来构建的,直到只有一个散列。这个创建的哈希被称为根,或merkle根。Merkle树可以通过log2(N)计算查看树中是否包含任何数据元素。

实现过程

准备交易数据

141557_FfOh_204117.png

创建合并左事务和右事务的散列数据

141602_LLGC_204117.png

执行循环直到只有一个散列

141618_aGiN_204117.png

算法和实现 

 

package com.goroom.merkle;
 
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
 
/**
 * Created by andyfeng on 2017/12/20.
 */
public class MerkleTrees {
    // transaction List
    List<String> txList;
    // Merkle Root
    String root;
 
    /**
     * constructor
     * @param txList transaction List 交易List
     */
    public MerkleTrees(List<String> txList) {
        this.txList = txList;
        root = "";
    }
 
    /**
     * execute merkle_tree and set root.
     */
    public void merkle_tree() {
 
        List<String> tempTxList = new ArrayList<String>();
 
        for (int i = 0; i < this.txList.size(); i++) {
            tempTxList.add(this.txList.get(i));
        }
 
        List<String> newTxList = getNewTxList(tempTxList);
 
        //执行循环,直到只剩下一个hash值
        while (newTxList.size() != 1) {
            newTxList = getNewTxList(newTxList);
        }
 
        this.root = newTxList.get(0);
    }
 
    /**
     * return Node Hash List.
     * @param tempTxList
     * @return
     */
    private List<String> getNewTxList(List<String> tempTxList) {
 
        List<String> newTxList = new ArrayList<String>();
        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;
    }
}

 

测试执行

package com.goroom.merkle;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * Created by andyfeng on 2017/12/20.
 */
public class App {
    public static void main(String [] args) {
        List<String> tempTxList = new ArrayList<String>();
        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());
    }
}

141852_TB8W_204117.png

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值