概要
Merkletreeは、ビットコインテクノロジーで広く使用されています。この記事は、コードを介して単純なMerkletreeを実装し、Merkle Treeのトリートを計算することを目的としています。
Merkle Treeは、コンピューター間で保存、処理、送信されたあらゆる種類のデータを検証するデータ構造です。
現在、Merkleツリーの主な目的は、ピアネットワークから受信されたデータブロックが損傷していないことを確認し、他のピアネットワークが偽のデータブロックを送信するために嘘をつかないことを確認することです。
マークルツリーアプリケーションの例
ビットコイン
gita
マゾンのダイナモ
ガッサンドラ
ビットコインのアプリケーション
ビットコインの各ブロックには、すべてのトランザクションのコレクション署名が含まれています。この署名は、Merkle Treeを使用して実装されています。マークルツリーは、ビットコインに使用されて、ブロック内のすべてのトランザクションを要約し、トランザクション全体の全体的なデジタルフィンガープリントを生成し、トランザクションがブロックに含まれているかどうかを確認するための非常に効果的なプロセスを提供します。
マークルツリーの非常に重要な使用は、ブロックに指定されたトランザクションが含まれているかどうかを確認することです。メルクルの木は、ハッシュが1つしかないまで、ノードのペアを再帰的にハッシュすることによって構築されます。
マークルツリーコードの実装
ハッシュツリーのノードは、Merkle Rootと呼ばれます。 Merkleツリーは、Log2(n)の時間の複雑さを使用して、ツリーにデータ要素が含まれているかどうかを確認できます。
パッケージテスト; Import java.security.messagegest; import java.util.arraylist; import java.util.list; public class merkletrees {//トランザクションリスト<string> txlist; // Merkle Root String root; / ** * constructor * @param txlistトランザクションリストトランザクションリスト */ public merkletrees(list <string> txlist){this.txlist = txlist; root = ""; } /*** merkle_treeを実行してルートを設定します。 */ 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(emtptXList); while(newtxlist.size()!= 1){newtxList = getNewtXList(newtxList); } this.root = newtxlist.get(0); } /***ノードハッシュリストを返します。 * @param temptxlist * @return */ private list <string> getNewtxList(list <string> emtptxlist){list <string> newtxlist = new arrayList <string>(); int index = 0; while(index <temptxlist.size()){//左の文字列左= temptxlist.get(index);インデックス++; //右文字列右= ""; if(index!= temptxlist.size()){right = temptxlist.get(index); } // sha2 hex value string sha2hexvalue = getsha2hexvalue(左 +右); newtxlist.add(sha2hexvalue);インデックス++; } newtxListを返します。 } / ** * return hex string * @param str * @return * / public string getsha2hexvalue(string str){byte [] cipher_byte; try {MessagedGest MD = MesagedGigest.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(); } 戻る ""; } / ** * get root * @return * / public string getRoot(){return this.root; }}データの準備
トランザクションデータをリストに入れます。
List <String> TemptXList = new ArrayList <String>(); emtptxList.add( "a"); semptxlist.add( "b"); emtptxlist.add( "c"); emtptxlist.add( "d"); emtptxlist.add( "e");
実装プロセス
トランザクションデータを準備して各データのハッシュ値を計算し、ツリーの左右のノードを徐々に形成してループを実行して、最終的に1つのデータのみが残っていることを知ることができます
プライベートリスト<String> getNewtXList(List <String> emtptXList){List <String> NewtxList = new ArrayList <String>(); int index = 0; while(index <temptxlist.size()){//左の文字列左= temptxlist.get(index);インデックス++; //右文字列右= ""; if(index!= temptxlist.size()){right = temptxlist.get(index); } // sha2 hex value string sha2hexvalue = getsha2hexvalue(左 +右); newtxlist.add(sha2hexvalue);インデックス++; }テスト
パッケージテスト; Import java.util.arraylist; Import java.util.list; 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()); }}実行結果
この記事では、単純なバイナリツリーの形からシンプルなメルクレーリーを実装し、トレアーフールを計算しますが、実際、メルクレーリーは保守的ではなく、バイナリツリーもマルチフォークの木である可能性があります。
この記事の90%は翻訳、元の住所に基づいています
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。