基于Java編寫第一個區(qū)塊鏈項目
前言
區(qū)塊鏈?zhǔn)菙?shù)字加密貨幣比特幣的核心技術(shù)。
區(qū)塊鏈?zhǔn)且粋€稱為塊的記錄列表,這些記錄使用鏈表鏈接在一起并使用加密技術(shù)。
每個數(shù)據(jù)塊都包含自己的數(shù)字指紋(稱為散列)、前一個數(shù)據(jù)塊的散列、時間戳和所做事務(wù)的數(shù)據(jù),使其在任何類型的數(shù)據(jù)泄露時都更加安全。
因此,如果一個塊的數(shù)據(jù)被改變,那么它的散列也會改變。如果散列被更改,那么它的散列將不同于下一個塊,下一個塊包含前一個塊的散列,影響它之后的所有塊的散列。更改哈希值,然后將其與其他塊進(jìn)行比較,這允許我們檢查區(qū)塊鏈。
區(qū)塊鏈實施:以下是區(qū)塊鏈實施中使用的功能。
1. 創(chuàng)建塊:要創(chuàng)建塊,將實現(xiàn)塊類。在類塊中:
- hash哈希將包含塊的哈希和
- previousHash將包含上一個塊的哈希。
- 字符串?dāng)?shù)據(jù)用于存儲塊的數(shù)據(jù)和
- long timeStamp用于存儲塊的時間戳。這里,long數(shù)據(jù)類型用于存儲毫秒數(shù)。
- calculateHash()生成散列
下面是類塊的實現(xiàn):
// Java implementation for creating // a block in a Blockchain import java.util.Date; public class Block { // Every block contains // a hash, previous hash and // data of the transaction made public String hash; public String previousHash; private String data; private long timeStamp; // Constructor for the block public Block(String data, String previousHash) { this.data = data; this.previousHash = previousHash; this.timeStamp = new Date().getTime(); this.hash = calculateHash(); } // Function to calculate the hash public String calculateHash() { // Calling the "crypt" class // to calculate the hash // by using the previous hash, // timestamp and the data String calculatedhash = crypt.sha256( previousHash + Long.toString(timeStamp) + data); return calculatedhash; } }
2. 生成哈希:要生成哈希,使用SHA256算法。
下面是算法的實現(xiàn)。
// Java program for Generating Hashes import java.security.MessageDigest; public class crypt { // Function that takes the string input // and returns the hashed string. public static String sha256(String input) { try { MessageDigest sha = MessageDigest .getInstance( "SHA-256"); int i = 0; byte[] hash = sha.digest( input.getBytes("UTF-8")); // hexHash will contain // the Hexadecimal hash StringBuffer hexHash = new StringBuffer(); while (i < hash.length) { String hex = Integer.toHexString( 0xff & hash[i]); if (hex.length() == 1) hexHash.append('0'); hexHash.append(hex); i++; } return hexHash.toString(); } catch (Exception e) { throw new RuntimeException(e); } } }
3. 存儲塊:現(xiàn)在,讓我們通過調(diào)用Block類的構(gòu)造函數(shù)將塊及其哈希值存儲在Block類型的ArrayList中。
// Java implementation to store // blocks in an ArrayList import java.util.ArrayList; public class GFG { // ArrayList to store the blocks public static ArrayList<Block> blockchain = new ArrayList<Block>(); // Driver code public static void main(String[] args) { // Adding the data to the ArrayList blockchain.add(new Block( "First block", "0")); blockchain.add(new Block( "Second block", blockchain .get(blockchain.size() - 1) .hash)); blockchain.add(new Block( "Third block", blockchain .get(blockchain.size() - 1) .hash)); blockchain.add(new Block( "Fourth block", blockchain .get(blockchain.size() - 1) .hash)); blockchain.add(new Block( "Fifth block", blockchain .get(blockchain.size() - 1) .hash)); } }
4. 區(qū)塊鏈有效性:最后,我們需要通過創(chuàng)建布爾方法來檢查區(qū)塊鏈的有效性。此方法將在“Main”類中實現(xiàn),并檢查散列是否等于計算的散列。如果所有哈希值都等于計算的哈希值,則該塊有效。
以下是有效性的實施情況:
// Java implementation to check // validity of the blockchain // Function to check // validity of the blockchain public static Boolean isChainValid() { Block currentBlock; Block previousBlock; // Iterating through // all the blocks for (int i = 1; i < blockchain.size(); i++) { // Storing the current block // and the previous block currentBlock = blockchain.get(i); previousBlock = blockchain.get(i - 1); // Checking if the current hash // is equal to the // calculated hash or not if (!currentBlock.hash .equals( currentBlock .calculateHash())) { System.out.println( "Hashes are not equal"); return false; } // Checking of the previous hash // is equal to the calculated // previous hash or not if (!previousBlock .hash .equals( currentBlock .previousHash)) { System.out.println( "Previous Hashes are not equal"); return false; } } // If all the hashes are equal // to the calculated hashes, // then the blockchain is valid return true; }
區(qū)塊鏈的優(yōu)勢
- Blokchain是一個分布式系統(tǒng)網(wǎng)絡(luò)。因此,數(shù)據(jù)泄露很難實施。
- 由于區(qū)塊鏈生成了每個區(qū)塊的散列,因此很難進(jìn)行惡意攻擊。
- 數(shù)據(jù)篡改將改變每個塊的哈希值,從而使區(qū)塊鏈無效
區(qū)塊鏈如何工作?
區(qū)塊鏈的基本單位是塊。一個塊能封裝多個事務(wù)或者其它有價值的數(shù)據(jù):
我們用哈希值表示一個塊。生成塊的哈希值叫做“挖掘”塊。挖掘塊通常在計算上很昂貴,因為它可以作為“工作證明”。
塊的哈希值通常由以下數(shù)據(jù)組成:
- 首先,塊的哈希值由封裝的事務(wù)組成。
- 哈希也由塊創(chuàng)建的時間戳組成
- 它還包括一個 nonce,一個在密碼學(xué)中使用的任意數(shù)字
- 最后,當(dāng)前塊的哈希也包括前一個塊的哈希
網(wǎng)絡(luò)中的多個節(jié)點(diǎn)可以同時對數(shù)據(jù)塊進(jìn)行挖掘。除了生成哈希外,節(jié)點(diǎn)還必須驗證添加到塊中的事務(wù)是否合法。先挖一個街區(qū),就贏了比賽!
總結(jié)
到此這篇關(guān)于Java實現(xiàn)區(qū)塊鏈的文章就介紹到這了,更多相關(guān)Java實現(xiàn)區(qū)塊鏈內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java14對于NullPointerException的新處理方式示例解析
這篇文章主要為大家介紹了Java14對于NullPointerException的新處理方式示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09Springboot+Mybatis實現(xiàn)分頁加條件查詢功能
這篇文章主要為大家詳細(xì)介紹了Springboot+Mybatis實現(xiàn)分頁加條件查詢,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04Springboot?整合?RabbitMQ?消息隊列?詳情
這篇文章主要介紹了Springboot整合RabbitMQ?消息隊列詳情,文章為榮啊主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08MyBatis動態(tài)Sql之if標(biāo)簽的用法詳解
這篇文章主要介紹了MyBatis動態(tài)Sql之if標(biāo)簽的用法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-07-07Java實現(xiàn)并發(fā)執(zhí)行定時任務(wù)并手動控制開始結(jié)束
這篇文章主要介紹了Java實現(xiàn)并發(fā)執(zhí)行定時任務(wù)并手動控制開始結(jié)束,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05