亚洲AVI,黑人巨茎大战欧美白妇,初高中生洗澡自慰高清网站,欧美日韩无砖专区一中文字

重慶分公司,新征程啟航

為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)

如何使用java代碼實(shí)現(xiàn)區(qū)塊鏈

小編給大家分享一下如何使用java代碼實(shí)現(xiàn)區(qū)塊鏈,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

成都服務(wù)器托管,成都創(chuàng)新互聯(lián)提供包括服務(wù)器租用、綿陽(yáng)電信機(jī)房、帶寬租用、云主機(jī)、機(jī)柜租用、主機(jī)租用托管、CDN網(wǎng)站加速、域名與空間等業(yè)務(wù)的一體化完整服務(wù)。電話咨詢:028-86922220

概述

MerkleTree被廣泛的應(yīng)用在比特幣技術(shù)中,本文旨在通過(guò)代碼實(shí)現(xiàn)一個(gè)簡(jiǎn)單的MerkleTree,并計(jì)算出Merkle tree的 TreeRoot。
Merkle Tree 是一種數(shù)據(jù)結(jié)構(gòu),用于驗(yàn)證在計(jì)算機(jī)之間和之間存儲(chǔ),處理和傳輸?shù)娜魏晤愋偷臄?shù)據(jù)。
目前,Merkle樹的主要用途是確保從對(duì)等網(wǎng)絡(luò)中接收的數(shù)據(jù)塊未受損和未改變,和檢查其他對(duì)等網(wǎng)絡(luò)沒(méi)有撒謊發(fā)送假數(shù)據(jù)塊。

如何使用java代碼實(shí)現(xiàn)區(qū)塊鏈

Merkle Tree應(yīng)用舉例

比特幣

GitA

mazon's Dynamo

Gassandra

比特幣中的應(yīng)用

比特幣中每個(gè)塊中都包含了所有交易的集合簽名,這個(gè)簽名就是用Merkle tree實(shí)現(xiàn)的,Merkle樹用于比特幣以匯總塊中的所有事務(wù),產(chǎn)生整個(gè)事務(wù)集合的整體數(shù)字指紋,提供非常有效的過(guò)程來(lái)驗(yàn)證事務(wù)是否包括在塊中。

如何使用java代碼實(shí)現(xiàn)區(qū)塊鏈 

Merkle樹一個(gè)很重要的用處是檢查塊中是否包含指定的交易,Merkle樹是通過(guò)遞歸哈希節(jié)點(diǎn)對(duì)來(lái)構(gòu)造的,直到只有一個(gè)哈希。

如何使用java代碼實(shí)現(xiàn)區(qū)塊鏈

Merkle tree 代碼實(shí)現(xiàn)

哈希樹的跟節(jié)點(diǎn)稱為Merkle根,Merkle樹可以僅用log2(N)的時(shí)間復(fù)雜度檢查任何一個(gè)數(shù)據(jù)元素是否包含在樹中:

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

 }

數(shù)據(jù)準(zhǔn)備

我們將交易的數(shù)據(jù),放入到List中:

List tempTxList = new ArrayList();
tempTxList.add("a");
tempTxList.add("b");
tempTxList.add("c");
tempTxList.add("d");
tempTxList.add("e");

實(shí)現(xiàn)過(guò)程

準(zhǔn)備交易數(shù)據(jù)
計(jì)算出每個(gè)數(shù)據(jù)的hash值,從左到右逐步組成樹的左右節(jié)點(diǎn)
執(zhí)行循環(huán)知道最后只剩下一個(gè)數(shù)據(jù)

如何使用java代碼實(shí)現(xiàn)區(qū)塊鏈

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++;
 }

測(cè)試

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

執(zhí)行結(jié)果

如何使用java代碼實(shí)現(xiàn)區(qū)塊鏈

以上是“如何使用java代碼實(shí)現(xiàn)區(qū)塊鏈”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


當(dāng)前標(biāo)題:如何使用java代碼實(shí)現(xiàn)區(qū)塊鏈
分享路徑:http://news.spvevtbd.cn/article/jgcgej.html

其他資訊

在線咨詢
服務(wù)熱線
服務(wù)熱線:028-86922220
TOP