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

前言
区块链是数字加密货币比特币的核心技术。
区块链是一个称为块的记录列表,这些记录使用链表链接在一起并使用加密技术。
每个数据块都包含自己的数字指纹(称为散列)、前一个数据块的散列、时间戳和所做事务的数据,使其在任何类型的数据泄露时都更加安全。
因此,如果一个块的数据被改变,那么它的散列也会改变。如果散列被更改,那么它的散列将不同于下一个块,下一个块包含前一个块的散列,影响它之后的所有块的散列。更改哈希值,然后将其与其他块进行比较,这允许我们检查区块链。
区块链实施:以下是区块链实施中使用的功能。

  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 = https://www.it610.com/article/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 blockchain = new ArrayList(); // 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是一个分布式系统网络。因此,数据泄露很难实施。
由于区块链生成了每个区块的散列,因此很难进行恶意攻击。
数据篡改将改变每个块的哈希值,从而使区块链无效
区块链如何工作?
区块链的基本单位是块。一个块能封装多个事务或者其它有价值的数据:
基于Java编写第一个区块链项目
文章图片

我们用哈希值表示一个块。生成块的哈希值叫做“挖掘”块。挖掘块通常在计算上很昂贵,因为它可以作为“工作证明”。
块的哈希值通常由以下数据组成:
首先,块的哈希值由封装的事务组成。
哈希也由块创建的时间戳组成
它还包括一个 nonce,一个在密码学中使用的任意数字
最后,当前块的哈希也包括前一个块的哈希
网络中的多个节点可以同时对数据块进行挖掘。除了生成哈希外,节点还必须验证添加到块中的事务是否合法。先挖一个街区,就赢了比赛!
【基于Java编写第一个区块链项目】总结
到此这篇关于Java实现区块链的文章就介绍到这了

    推荐阅读