我是靠谱客的博主 等待水池,最近开发中收集的这篇文章主要介绍Java实现区块链 --- 代码篇首先弄清楚需要哪些信息创建一个Block类创建一个工具类创建一个测试类结果源码地址,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

首先弄清楚需要哪些信息

自身的hash值,上一个区块的hash值,保存的数据信息,时间戳,随机数。

创建一个Block类

package blockChain;

import java.util.Date;

/**
 * @author WuYongheng
 * @date 2022/10/22
 * @description
 */
public class Block {
    public String hash;
    public String previousHash;
    private String data;
    private long timeStamp;
    private int nonce;

    /**
     * 区块构造函数
     *
     * @param previousHash
     * @param data
     */
    public Block(String data, String previousHash) {
        this.previousHash = previousHash;
        this.data = data;
        this.timeStamp = new Date().getTime();
        this.hash = calculateHash();
    }

    /**
     * 计算区块的hash值,使用SHA256加密算法
     * nonce每加一次,都会调用一次applySha256()方法
     *
     * @return calculateHash
     */
    private String calculateHash() {
        String calculateHash = StringUtil.applySha256(
                previousHash
                        + Long.toString(timeStamp)
                        + data
                        + Integer.toString(nonce)
        );
        return calculateHash;
    }

    /**
     * 通过随机数nonce的递增,不断地尝试,得到符合要求的哈希值,使用SHA256加密
     * 人为制造哈希碰撞的可能性很低,可以认为随机数是唯一的
     *
     * @param difficulty 困难度,表示哈希值的开头有多少个零
     */
    public void mineBlock(int difficulty) {
        String target = StringUtil.getDifficultyString(difficulty);
        while (!hash.substring(0, difficulty).equals(target)) {
            nonce++;
            hash = calculateHash();
        }
        System.out.println("Block Mined!!! : " + hash);
    }

}

创建一个工具类

需要手动添加一下gson的jar包

package blockChain;

import com.google.gson.GsonBuilder;

import java.security.MessageDigest;

/**
 * @author WuYongheng
 * @date 2022/10/22
 * @description 工具类,负责加密,返回‘困难度’个零,解析json数据
 */
public class StringUtil {
    /**
     * for循环的解释:0xff & hash[i]
     *
     * @param input 未加密的字符串
     * @return 加密之后的字符串
     */
    public static String applySha256(String input) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
//            传入需要计算的字符串,把输入变成ascii值
            byte[] hash = digest.digest(input.getBytes("UTF-8"));
            StringBuffer hexString = new StringBuffer();

            for (int i = 0; i < hash.length; i++) {
                String hex = Integer.toHexString(0xff & hash[i]);
                if (hex.length() == 1) {
                    hexString.append('0');
                } 
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 困难度是多少,就返回多少个零
     * 比如 difficulty=6 返回 000000
     * @param difficulty 困难度
     * @return 一个字符串
     */
    public static String getDifficultyString(int difficulty) {
        return new String(new char[difficulty]).replace('', '0');
    }

    /**
     * 将数据以json的格式显示
     * @param o
     * @return
     */
    public static String getJson(Object o) {
        return new GsonBuilder().setPrettyPrinting().create().toJson(o);
    }

}

单独解释一下for循环

            for (int i = 0; i < hash.length; i++) {
                String hex = Integer.toHexString(0xff & hash[i]);
                if (hex.length() == 1) {
                    hexString.append('0');
                } 
                hexString.append(hex);
            }

理解:0xff & hash[i] 与 操作  hexString.append('0')  补零操作

有了这两步,就能确保得到一共64位的哈希值,每一位上的数可以转换成4位2进制数,比如a转换成1010,64*4一共256位0和1组成的哈希值。

创建一个测试类

package blockChain;

import java.util.ArrayList;

/**
 * @author WuYongheng
 * @date 2022/10/22
 * @description 测试类
 */
public class TestBlockChain {

    public static ArrayList<Block> blockchain = new ArrayList<Block>();
    // 设置挖矿困难度为5,会体现在哈希值,以5个0开头,困难度越大,计算花的时间越多
    public static int difficulty = 5;

    /**
     * 手动创建3个区块,将区块的内容以json的格式展示
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("Trying to Mine block 1... ");
        // 创世块,前一个区块的哈希值为 0
        addBlock(new Block("Hi im the first block", "0"));

        System.out.println("Trying to Mine block 2... ");
        addBlock(new Block("Yo im the second block", blockchain.get(blockchain.size() - 1).hash));

        System.out.println("Trying to Mine block 3... ");
        addBlock(new Block("Hey im the third block", blockchain.get(blockchain.size() - 1).hash));

        String blockchainJson = StringUtil.getJson(blockchain);
        System.out.println("nThe block chain: ");
        System.out.println(blockchainJson);

    }

    /**
     * 创建新的区块
     * @param newBlock
     */
    private static void addBlock(Block newBlock) {
        newBlock.mineBlock(difficulty);
        blockchain.add(newBlock);
    }
}

结果

"C:Program FilesJavajdk1.8.0_221binjava.exe" "-javaagent:D:devenvIntelliJ IDEA 2021.1.1libidea_rt.jar=51144:D:devenvIntelliJ IDEA 2021.1.1bin" -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.8.0_221jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_221jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_221jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_221jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_221jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_221jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_221jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_221jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_221jrelibextnashorn.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_221jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_221jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_221jrelibjce.jar;C:Program FilesJavajdk1.8.0_221jrelibjfr.jar;C:Program FilesJavajdk1.8.0_221jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_221jrelibjsse.jar;C:Program FilesJavajdk1.8.0_221jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_221jrelibplugin.jar;C:Program FilesJavajdk1.8.0_221jrelibresources.jar;C:Program FilesJavajdk1.8.0_221jrelibrt.jar;D:codebeforecsdncodeoutproductionCSDNcode;C:UsersAdministratorDesktop学习笔记老刘jar包gson-2.8.2.jar" blockChain.TestBlockChain
Trying to Mine block 1... 
Block Mined!!! : 0000071d5ed7e99267ffbe73df5b01affe62928347bbe4032187f20fac1c8bb0
Trying to Mine block 2... 
Block Mined!!! : 00000015080301d4e8a24786cc9ed85949fc96607d28373e04dfd5dd003a0233
Trying to Mine block 3... 
Block Mined!!! : 0000096564160a2b6758839900780138760fb210044cb8685715646e4b511534

The block chain: 
[
  {
    "hash": "0000071d5ed7e99267ffbe73df5b01affe62928347bbe4032187f20fac1c8bb0",
    "previousHash": "0",
    "data": "Hi im the first block",
    "timeStamp": 1666431244665,
    "nonce": 1214985
  },
  {
    "hash": "00000015080301d4e8a24786cc9ed85949fc96607d28373e04dfd5dd003a0233",
    "previousHash": "0000071d5ed7e99267ffbe73df5b01affe62928347bbe4032187f20fac1c8bb0",
    "data": "Yo im the second block",
    "timeStamp": 1666431246529,
    "nonce": 656899
  },
  {
    "hash": "0000096564160a2b6758839900780138760fb210044cb8685715646e4b511534",
    "previousHash": "00000015080301d4e8a24786cc9ed85949fc96607d28373e04dfd5dd003a0233",
    "data": "Hey im the third block",
    "timeStamp": 1666431247645,
    "nonce": 157564
  }
]

Process finished with exit code 0

使用在线加密工具验证

以第一个区块为例

源码地址

https://gitee.com/ipkiss/csdncode

参考

sunny_ice

最后

以上就是等待水池为你收集整理的Java实现区块链 --- 代码篇首先弄清楚需要哪些信息创建一个Block类创建一个工具类创建一个测试类结果源码地址的全部内容,希望文章能够帮你解决Java实现区块链 --- 代码篇首先弄清楚需要哪些信息创建一个Block类创建一个工具类创建一个测试类结果源码地址所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部