经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 其他 » 区块链 » 查看文章
基于Java编写第一个区块链项目
来源:jb51  时间:2021/8/26 12:38:11  对本文有异议

前言

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

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

每个数据块都包含自己的数字指纹(称为散列)、前一个数据块的散列、时间戳和所做事务的数据,使其在任何类型的数据泄露时都更加安全。

因此,如果一个块的数据被改变,那么它的散列也会改变。如果散列被更改,那么它的散列将不同于下一个块,下一个块包含前一个块的散列,影响它之后的所有块的散列。更改哈希值,然后将其与其他块进行比较,这允许我们检查区块链。

区块链实施:以下是区块链实施中使用的功能。

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

  • hash哈希将包含块的哈希和
  • previousHash将包含上一个块的哈希。
  • 字符串数据用于存储块的数据和
  • long timeStamp用于存储块的时间戳。这里,long数据类型用于存储毫秒数。
  • calculateHash()生成散列

下面是类块的实现:

  1. // Java implementation for creating
  2. // a block in a Blockchain
  3. import java.util.Date;
  4. public class Block {
  5. // Every block contains
  6. // a hash, previous hash and
  7. // data of the transaction made
  8. public String hash;
  9. public String previousHash;
  10. private String data;
  11. private long timeStamp;
  12. // Constructor for the block
  13. public Block(String data,
  14. String previousHash)
  15. {
  16. this.data = data;
  17. this.previousHash
  18. = previousHash;
  19. this.timeStamp
  20. = new Date().getTime();
  21. this.hash
  22. = calculateHash();
  23. }
  24. // Function to calculate the hash
  25. public String calculateHash()
  26. {
  27. // Calling the "crypt" class
  28. // to calculate the hash
  29. // by using the previous hash,
  30. // timestamp and the data
  31. String calculatedhash
  32. = crypt.sha256(
  33. previousHash
  34. + Long.toString(timeStamp)
  35. + data);
  36. return calculatedhash;
  37. }
  38. }

2. 生成哈希:要生成哈希,使用SHA256算法。

下面是算法的实现。

  1. // Java program for Generating Hashes
  2. import java.security.MessageDigest;
  3. public class crypt {
  4. // Function that takes the string input
  5. // and returns the hashed string.
  6. public static String sha256(String input)
  7. {
  8. try {
  9. MessageDigest sha
  10. = MessageDigest
  11. .getInstance(
  12. "SHA-256");
  13. int i = 0;
  14. byte[] hash
  15. = sha.digest(
  16. input.getBytes("UTF-8"));
  17. // hexHash will contain
  18. // the Hexadecimal hash
  19. StringBuffer hexHash
  20. = new StringBuffer();
  21. while (i < hash.length) {
  22. String hex
  23. = Integer.toHexString(
  24. 0xff & hash[i]);
  25. if (hex.length() == 1)
  26. hexHash.append('0');
  27. hexHash.append(hex);
  28. i++;
  29. }
  30. return hexHash.toString();
  31. }
  32. catch (Exception e) {
  33. throw new RuntimeException(e);
  34. }
  35. }
  36. }

3. 存储块:现在,让我们通过调用Block类的构造函数将块及其哈希值存储在Block类型的ArrayList中。

  1. // Java implementation to store
  2. // blocks in an ArrayList
  3. import java.util.ArrayList;
  4. public class GFG {
  5. // ArrayList to store the blocks
  6. public static ArrayList<Block> blockchain
  7. = new ArrayList<Block>();
  8. // Driver code
  9. public static void main(String[] args)
  10. {
  11. // Adding the data to the ArrayList
  12. blockchain.add(new Block(
  13. "First block", "0"));
  14. blockchain.add(new Block(
  15. "Second block",
  16. blockchain
  17. .get(blockchain.size() - 1)
  18. .hash));
  19. blockchain.add(new Block(
  20. "Third block",
  21. blockchain
  22. .get(blockchain.size() - 1)
  23. .hash));
  24. blockchain.add(new Block(
  25. "Fourth block",
  26. blockchain
  27. .get(blockchain.size() - 1)
  28. .hash));
  29. blockchain.add(new Block(
  30. "Fifth block",
  31. blockchain
  32. .get(blockchain.size() - 1)
  33. .hash));
  34. }
  35. }

4. 区块链有效性:最后,我们需要通过创建布尔方法来检查区块链的有效性。此方法将在“Main”类中实现,并检查散列是否等于计算的散列。如果所有哈希值都等于计算的哈希值,则该块有效。

以下是有效性的实施情况:

  1. // Java implementation to check
  2. // validity of the blockchain
  3. // Function to check
  4. // validity of the blockchain
  5. public static Boolean isChainValid()
  6. {
  7. Block currentBlock;
  8. Block previousBlock;
  9. // Iterating through
  10. // all the blocks
  11. for (int i = 1;
  12. i < blockchain.size();
  13. i++) {
  14. // Storing the current block
  15. // and the previous block
  16. currentBlock = blockchain.get(i);
  17. previousBlock = blockchain.get(i - 1);
  18. // Checking if the current hash
  19. // is equal to the
  20. // calculated hash or not
  21. if (!currentBlock.hash
  22. .equals(
  23. currentBlock
  24. .calculateHash())) {
  25. System.out.println(
  26. "Hashes are not equal");
  27. return false;
  28. }
  29. // Checking of the previous hash
  30. // is equal to the calculated
  31. // previous hash or not
  32. if (!previousBlock
  33. .hash
  34. .equals(
  35. currentBlock
  36. .previousHash)) {
  37. System.out.println(
  38. "Previous Hashes are not equal");
  39. return false;
  40. }
  41. }
  42. // If all the hashes are equal
  43. // to the calculated hashes,
  44. // then the blockchain is valid
  45. return true;
  46. }

区块链的优势

  • Blokchain是一个分布式系统网络。因此,数据泄露很难实施。
  • 由于区块链生成了每个区块的散列,因此很难进行恶意攻击。
  • 数据篡改将改变每个块的哈希值,从而使区块链无效

区块链如何工作?

区块链的基本单位是块。一个块能封装多个事务或者其它有价值的数据:


我们用哈希值表示一个块。生成块的哈希值叫做“挖掘”块。挖掘块通常在计算上很昂贵,因为它可以作为“工作证明”。

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

  • 首先,块的哈希值由封装的事务组成。
  • 哈希也由块创建的时间戳组成
  • 它还包括一个 nonce,一个在密码学中使用的任意数字
  • 最后,当前块的哈希也包括前一个块的哈希

网络中的多个节点可以同时对数据块进行挖掘。除了生成哈希外,节点还必须验证添加到块中的事务是否合法。先挖一个街区,就赢了比赛!

总结

到此这篇关于Java实现区块链的文章就介绍到这了,更多相关Java实现区块链内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号