개요
Merkletree는 비트 코인 기술에 널리 사용됩니다. 이 기사는 코드를 통해 간단한 Merkletree를 구현하고 Merkle Tree의 트리 루트를 계산하는 것을 목표로합니다.
Merkle Tree는 컴퓨터간에 저장, 처리 및 전송 된 모든 유형의 데이터를 확인하는 데이터 구조입니다.
현재 Merkle Tree의 주요 목적은 피어 네트워크에서받은 데이터 블록이 손상되고 변경되지 않은지 확인하고 다른 피어 네트워크가 가짜 데이터 블록을 보내지 않도록하는 것입니다.
머클 트리 애플리케이션 예제
비트 코인
지타
마손의 다이너 모
가사드라
비트 코인의 응용 프로그램
비트 코인의 각 블록에는 모든 트랜잭션의 컬렉션 서명이 포함되어 있습니다. 이 서명은 Merkle Tree를 사용하여 구현됩니다. Merkle Tree는 비트 코인에 사용되어 블록의 모든 트랜잭션을 요약하고 전체 트랜잭션 세트의 전체 디지털 지문을 생성하며 트랜잭션이 블록에 포함되어 있는지 확인하는 매우 효과적인 프로세스를 제공합니다.
Merkle Trees의 매우 중요한 사용은 블록에 지정된 트랜잭션이 포함되어 있는지 확인하는 것입니다. 메르클 트리는 해시가 하나만있을 때까지 노드 쌍의 노드 쌍에 의해 재귀 적으로 해외로 구성됩니다.
머클 트리 코드 구현
해시 트리의 노드를 머클 루트라고합니다. Merkle Tree는 Log2 (n)의 시간 복잡성을 사용하여 트리에 데이터 요소가 포함되어 있는지 확인할 수 있습니다.
패키지 테스트; import java.security.messagegegest; import java.util.arraylist; import java.util.list; public class merkletrees {// transaction list <string> txlist; // 머클 루트 문자열 루트; / ** * 생성자 * @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 (temptxList); while (newtxlist.size ()! = 1) {newtxList = getNewtXlist (newtxList); } this.root = newtxList.get (0); } /*** 반환 노드 해시 목록. * @param temptxList * @return */ private list <string> getNewtxList (list <string> temptxList) {list <string> newtxList = new ArrayList <String> (); int index = 0; while (index <temptxList.size ()) {// 왼쪽 줄 왼쪽 = temptxList.get (index); 색인 ++; // 오른쪽 문자열 right = ""; if (index! = temptxList.size ()) {right = temptxList.get (index); } // SHA2 16 진수 값 문자열 sha2HexValue = getsha2HexValue (왼쪽 + 오른쪽); newtxList.Add (SHA2HexValue); 색인 ++; } 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 (예외 e) {e.printstacktrace (); } 반품 ""; } / ** * get root * @return * / public string getRoot () {return this.Root; }}데이터 준비
트랜잭션 데이터를 목록에 넣습니다.
list <string> temptxList = new ArrayList <string> (); temptXList.Add ( "a"); temptxList.add ( "b"); temptxList.add ( "c"); temptxList.add ( "d"); temptxList.add ( "e");
구현 프로세스
트랜잭션 데이터를 준비하여 각 데이터의 해시 값을 계산하고 트리의 왼쪽 및 오른쪽 노드를 점차적으로 형성하여 루프를 실행하여 끝에 하나의 데이터 만 남았다는 것을 알 수 있습니다.
개인 목록 <string> getNewtXlist (list <string> temptxList) {list <string> newtxList = new ArrayList <string> (); int index = 0; while (index <temptxList.size ()) {// 왼쪽 줄 왼쪽 = temptxList.get (index); 색인 ++; // 오른쪽 문자열 right = ""; if (index! = temptxList.size ()) {right = temptxList.get (index); } // SHA2 16 진수 값 문자열 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 = 새로운 merkletrees (temptxList); merkletrees.merkle_tree (); System.out.println ( "루트 :" + merkletrees.getRoot ()); }}실행 결과
이 기사는 간단한 이진 트리의 형태에서 간단한 merkletree를 구현하고 Treeroot를 계산하지만 실제로 Merkletree는 보수적이지 않으며 이진 트리도 멀티 포크 나무 일 수 있습니다.
이 기사의 90%는 번역, 원본 주소를 기반으로합니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.