溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java如何編寫區(qū)塊鏈項(xiàng)目

發(fā)布時(shí)間:2021-08-26 13:55:43 來源:億速云 閱讀:106 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“Java如何編寫區(qū)塊鏈項(xiàng)目”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Java如何編寫區(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)建塊:要?jiǎ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ù):

Java如何編寫區(qū)塊鏈項(xiàng)目

我們用哈希值表示一個(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ū),就贏了比賽!

以上是“Java如何編寫區(qū)塊鏈項(xiàng)目”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI