基于Java編寫(xiě)第一個(gè)區(qū)塊鏈項(xiàng)目
前言
區(qū)塊鏈?zhǔn)菙?shù)字加密貨幣比特幣的核心技術(shù)。
區(qū)塊鏈?zhǔn)且粋€(gè)稱(chēng)為塊的記錄列表,這些記錄使用鏈表鏈接在一起并使用加密技術(shù)。
每個(gè)數(shù)據(jù)塊都包含自己的數(shù)字指紋(稱(chēng)為散列)、前一個(gè)數(shù)據(jù)塊的散列、時(shí)間戳和所做事務(wù)的數(shù)據(jù),使其在任何類(lèi)型的數(shù)據(jù)泄露時(shí)都更加安全。
因此,如果一個(gè)塊的數(shù)據(jù)被改變,那么它的散列也會(huì)改變。如果散列被更改,那么它的散列將不同于下一個(gè)塊,下一個(gè)塊包含前一個(gè)塊的散列,影響它之后的所有塊的散列。更改哈希值,然后將其與其他塊進(jìn)行比較,這允許我們檢查區(qū)塊鏈。
區(qū)塊鏈實(shí)施:以下是區(qū)塊鏈實(shí)施中使用的功能。
1. 創(chuàng)建塊:要?jiǎng)?chuàng)建塊,將實(shí)現(xiàn)塊類(lèi)。在類(lèi)塊中:
- hash哈希將包含塊的哈希和
- previousHash將包含上一個(gè)塊的哈希。
- 字符串?dāng)?shù)據(jù)用于存儲(chǔ)塊的數(shù)據(jù)和
- long timeStamp用于存儲(chǔ)塊的時(shí)間戳。這里,long數(shù)據(jù)類(lèi)型用于存儲(chǔ)毫秒數(shù)。
- calculateHash()生成散列
下面是類(lèi)塊的實(shí)現(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算法。
下面是算法的實(shí)現(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. 存儲(chǔ)塊:現(xiàn)在,讓我們通過(guò)調(diào)用Block類(lèi)的構(gòu)造函數(shù)將塊及其哈希值存儲(chǔ)在Block類(lèi)型的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ū)塊鏈有效性:最后,我們需要通過(guò)創(chuàng)建布爾方法來(lái)檢查區(qū)塊鏈的有效性。此方法將在“Main”類(lèi)中實(shí)現(xiàn),并檢查散列是否等于計(jì)算的散列。如果所有哈希值都等于計(jì)算的哈希值,則該塊有效。
以下是有效性的實(shí)施情況:
// 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)勢(shì)
- Blokchain是一個(gè)分布式系統(tǒng)網(wǎng)絡(luò)。因此,數(shù)據(jù)泄露很難實(shí)施。
- 由于區(qū)塊鏈生成了每個(gè)區(qū)塊的散列,因此很難進(jìn)行惡意攻擊。
- 數(shù)據(jù)篡改將改變每個(gè)塊的哈希值,從而使區(qū)塊鏈無(wú)效
區(qū)塊鏈如何工作?
區(qū)塊鏈的基本單位是塊。一個(gè)塊能封裝多個(gè)事務(wù)或者其它有價(jià)值的數(shù)據(jù):

我們用哈希值表示一個(gè)塊。生成塊的哈希值叫做“挖掘”塊。挖掘塊通常在計(jì)算上很昂貴,因?yàn)樗梢宰鳛椤肮ぷ髯C明”。
塊的哈希值通常由以下數(shù)據(jù)組成:
- 首先,塊的哈希值由封裝的事務(wù)組成。
- 哈希也由塊創(chuàng)建的時(shí)間戳組成
- 它還包括一個(gè) nonce,一個(gè)在密碼學(xué)中使用的任意數(shù)字
- 最后,當(dāng)前塊的哈希也包括前一個(gè)塊的哈希
網(wǎng)絡(luò)中的多個(gè)節(jié)點(diǎn)可以同時(shí)對(duì)數(shù)據(jù)塊進(jìn)行挖掘。除了生成哈希外,節(jié)點(diǎn)還必須驗(yàn)證添加到塊中的事務(wù)是否合法。先挖一個(gè)街區(qū),就贏(yíng)了比賽!
總結(jié)
到此這篇關(guān)于Java實(shí)現(xiàn)區(qū)塊鏈的文章就介紹到這了,更多相關(guān)Java實(shí)現(xiàn)區(qū)塊鏈內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java14對(duì)于NullPointerException的新處理方式示例解析
這篇文章主要為大家介紹了Java14對(duì)于NullPointerException的新處理方式示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Springboot+Mybatis實(shí)現(xiàn)分頁(yè)加條件查詢(xún)功能
這篇文章主要為大家詳細(xì)介紹了Springboot+Mybatis實(shí)現(xiàn)分頁(yè)加條件查詢(xún),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Springboot?整合?RabbitMQ?消息隊(duì)列?詳情
這篇文章主要介紹了Springboot整合RabbitMQ?消息隊(duì)列詳情,文章為榮啊主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
MyBatis動(dòng)態(tài)Sql之if標(biāo)簽的用法詳解
這篇文章主要介紹了MyBatis動(dòng)態(tài)Sql之if標(biāo)簽的用法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-07-07
Java實(shí)現(xiàn)并發(fā)執(zhí)行定時(shí)任務(wù)并手動(dòng)控制開(kāi)始結(jié)束
這篇文章主要介紹了Java實(shí)現(xiàn)并發(fā)執(zhí)行定時(shí)任務(wù)并手動(dòng)控制開(kāi)始結(jié)束,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
解決Spring在Thread中注入Bean無(wú)效的問(wèn)題
這篇文章主要介紹了解決Spring在Thread中注入Bean無(wú)效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02

