关于javascript:基于Java编写第一个区块链项目

10次阅读

共计 3583 个字符,预计需要花费 9 分钟才能阅读完成。

前言
区块链是数字加密货币比特币的核心技术。

区块链是一个称为块的记录列表,这些记录应用链表链接在一起并应用加密技术。

每个数据块都蕴含本人的数字指纹(称为散列)、前一个数据块的散列、工夫戳和所做事务的数据,使其在任何类型的数据泄露时都更加平安。

因而,如果一个块的数据被扭转,那么它的散列也会扭转。如果散列被更改,那么它的散列将不同于下一个块,下一个块蕴含前一个块的散列,影响它之后的所有块的散列。更改哈希值,而后将其与其余块进行比拟,这容许咱们查看区块链。

区块链施行:以下是区块链施行中应用的性能。

  1. 创立块:要创立块,将实现块类。在类块中:

hash 哈希将蕴含块的哈希和
previousHash 将蕴含上一个块的哈希。
字符串数据用于存储块的数据和
long timeStamp 用于存储块的工夫戳。这里,long 数据类型用于存储毫秒数。
calculateHash()生成散列
上面是类块的实现:

// 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;
    }
}
  1. 生成哈希:要生成哈希,应用 SHA256 算法。

上面是算法的实现。

// 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);
        }
    }
}
  1. 存储块:当初,让咱们通过调用 Block 类的构造函数将块及其哈希值存储在 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));
    }
}
  1. 区块链有效性:最初,咱们须要通过创立布尔方法来查看区块链的有效性。此办法将在“Main”类中实现,并查看散列是否等于计算的散列。如果所有哈希值都等于计算的哈希值,则该块无效。

以下是有效性的施行状况:

// 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;
}

区块链的劣势
Blokchain 是一个分布式系统网络。因而,数据泄露很难施行。
因为区块链生成了每个区块的散列,因而很难进行歹意攻打。
数据篡改将扭转每个块的哈希值,从而使区块链有效
区块链如何工作?
区块链的根本单位是块。一个块能封装多个事务或者其它有价值的数据:

咱们用哈希值示意一个块。生成块的哈希值叫做“开掘”块。开掘块通常在计算上很低廉,因为它能够作为“工作证实”。

块的哈希值通常由以下数据组成:

首先,块的哈希值由封装的事务组成。
哈希也由块创立的工夫戳组成
它还包含一个 nonce,一个在密码学中应用的任意数字
最初,以后块的哈希也包含前一个块的哈希
网络中的多个节点能够同时对数据块进行开掘。除了生成哈希外,节点还必须验证增加到块中的事务是否非法。先挖一个街区,就赢了较量!

总结
到此这篇对于 Java 实现区块链的文章就介绍到这了

正文完
 0