主页 > 海外版imtoken > 基于Java编写第一个区块链项目
基于Java编写第一个区块链项目
前言
区块链是数字加密货币比特币的核心技术。
区块链是称为块的记录列表,这些记录使用链表和密码学链接在一起。
每个数据块都包含自己的数字指纹(称为散列)、前一个块的散列、时间戳和交易数据第一枚比特币区块记录的是什么内容,使其在发生任何类型的数据泄露时更加安全。
因此,如果一个区块的数据发生变化,它的哈希值也会发生变化。 如果更改了哈希值,那么它将具有与下一个块不同的哈希值,下一个块包含前一个块的哈希值,影响它之后所有块的哈希值。 更改哈希值然后将其与其他块进行比较可以让我们检查区块链。
区块链实施:以下是区块链实施中使用的功能。
1. 创建区块:要创建区块第一枚比特币区块记录的是什么内容,将实现区块类。 在类块中:
这是类块的实现:
// 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 算法。
下面是算法的实现。
// 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. 存储块: 现在,让我们通过调用 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 ArrayListblockchain = 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)); } }
4. 区块链有效性:最后,我们需要通过创建一个布尔方法来检查区块链的有效性。 此方法将在“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; }
区块链的优势 区块链如何运作?
区块链的基本单位是区块。 一个区块可以封装多个交易或其他有价值的数据:
我们用哈希值表示一个块。 生成块的散列称为“挖掘”块。 挖掘一个块通常在计算上是昂贵的,因为它充当“工作量证明”。
一个区块的哈希值通常由以下数据组成:
网络中的多个节点可以同时挖掘数据块。 除了生成哈希之外,节点还必须验证添加到块中的交易是否合法。 先挖一个方块,你就赢了!
总结
关于用 Java 实现区块链的文章到此结束。 关于Java实现区块链的更多信息,请搜索我们之前的文章或继续浏览下面的相关文章。 希望大家以后多多支持!