基于Java編寫第一個(gè)區(qū)塊鏈項(xiàng)目
前言
區(qū)塊鏈?zhǔn)菙?shù)字加密貨幣比特幣的核心技術(shù)。
區(qū)塊鏈?zhǔn)且粋€(gè)稱為塊的記錄列表,這些記錄使用鏈表鏈接在一起并使用加密技術(shù)。
每個(gè)數(shù)據(jù)塊都包含自己的數(shù)字指紋(稱為散列)、前一個(gè)數(shù)據(jù)塊的散列、時(shí)間戳和所做事務(wù)的數(shù)據(jù),使其在任何類型的數(shù)據(jù)泄露時(shí)都更加安全。
因此,如果一個(gè)塊的數(shù)據(jù)被改變,那么它的散列也會改變。如果散列被更改,那么它的散列將不同于下一個(gè)塊,下一個(gè)塊包含前一個(gè)塊的散列,影響它之后的所有塊的散列。更改哈希值,然后將其與其他塊進(jìn)行比較,這允許我們檢查區(qū)塊鏈。
區(qū)塊鏈實(shí)施:以下是區(qū)塊鏈實(shí)施中使用的功能。
1. 創(chuàng)建塊:要創(chuàng)建塊,將實(shí)現(xiàn)塊類。在類塊中:
- hash哈希將包含塊的哈希和
- previousHash將包含上一個(gè)塊的哈希。
- 字符串?dāng)?shù)據(jù)用于存儲塊的數(shù)據(jù)和
- long timeStamp用于存儲塊的時(shí)間戳。這里,long數(shù)據(jù)類型用于存儲毫秒數(shù)。
- calculateHash()生成散列
下面是類塊的實(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. 存儲塊:現(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”類中實(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)勢
- Blokchain是一個(gè)分布式系統(tǒng)網(wǎng)絡(luò)。因此,數(shù)據(jù)泄露很難實(shí)施。
- 由于區(qū)塊鏈生成了每個(gè)區(qū)塊的散列,因此很難進(jìn)行惡意攻擊。
- 數(shù)據(jù)篡改將改變每個(gè)塊的哈希值,從而使區(qū)塊鏈無效
區(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í)對數(shù)據(jù)塊進(jìn)行挖掘。除了生成哈希外,節(jié)點(diǎn)還必須驗(yàn)證添加到塊中的事務(wù)是否合法。先挖一個(gè)街區(qū),就贏了比賽!
總結(jié)
到此這篇關(guān)于Java實(shí)現(xiàn)區(qū)塊鏈的文章就介紹到這了,更多相關(guān)Java實(shí)現(xiàn)區(qū)塊鏈內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談web項(xiàng)目讀取classpath路徑下面的文件
這篇文章主要介紹了淺談web項(xiàng)目讀取classpath路徑下面的文件,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
如何將Java與C#時(shí)間進(jìn)行互相轉(zhuǎn)換
這篇文章主要介紹了Java與C#時(shí)間互轉(zhuǎn)的方法以及JAVA日期、C#日期計(jì)算說明,需要的朋友可以參考下2022-11-11
以Java代碼的方式總結(jié)幾個(gè)典型的內(nèi)存溢出案例
作為程序員,多多少少都會遇到一些內(nèi)存溢出的場景,如果你還沒遇到,說明你工作的年限可能比較短,或者你根本就是個(gè)假程序員!哈哈,開個(gè)玩笑.今天分享給大家Java內(nèi)存溢出的相關(guān)案例,希望大家在日常工作中,盡量避免寫這些low水平的代碼,需要的朋友可以參考下2021-06-06

