大家好,华春来为大家解答区块链的实现的相关区块链备案知识。区块链的实现很多人还不知道,现在让我们一起来看看吧!
区块链就是一串或者是一系列区块的集合,类似于链表的概念,每个区块都指向于后面一个区块,然后顺序的连接在一起。在区块链中的每一个区块都存放了很多很有价值的信息,主要包括三个部分:自己的数字签名,上一个区块的数字签名,还有一切需要加密的数据(这些数据在比特币中就相当于是交易的信息,它是加密货币的本质)。每个数字签名不但证明了自己是特有的一个区块,而且指向了前一个区块的来源,让所有的区块在链条中可以串起来,而数据就是一些特定的信息,你可以按照业务逻辑来保存业务数据。
这里的hash指的就是数字签名
所以每一个区块不仅包含前一个区块的hash值,同时包含自身的一个hash值,自身的hash值是通过之前的hash值和数据data通过hash计算出来的。如果前一个区块的数据一旦被篡改了,那么前一个区块的hash值也会同样发生变化(因为数据也被计算在内),这样也就导致了所有后续的区块中的hash值。所以计算和比对hash值会让我们检查到当前的区块链是否是有效的,也就避免了数据被恶意篡改的可能性,因为篡改数据就会改变hash值并破坏整个区块链。
/**
* 区块
*/
@ToString
@Getter
public class Block {
//数字签名
private String hash;
//上一个区块的数字签名
private String preHash;
//保存的数据
private String data;
//时间戳
private long timeStamp;
//工作量证明
private int nonce;
public Block(String data,String preHash) {
this.data = data;
this.preHash = preHash;
timeStamp = new Date().getTime();
hash = calculateHash();
}
/**
* 计算数字签名
* @return
*/
public String calculateHash() {
return StringUntil.applySha256(preHash + timeStamp + nonce + data);
}
/**
* 挖矿
* @param difficuity 挖矿难度
*/
public void mineBlock(int difficuity) {
String target = new String(new char[difficuity]).replace('\0','0');
while (!hash.substring(0,difficuity).equals(target)) {
nonce++;
hash = calculateHash();
}
System.out.println("Block Mind!!!: " + hash);
}
}
public class StringUntil {
/**
* Sha256离散
* @param input
* @return
*/
public static String applySha256(String input){
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
//Applies sha256 to our input,
byte[] hash = digest.digest(input.getBytes("UTF-8"));
StringBuffer hexString = new StringBuffer(); // This will contain hash as hexidecimal
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);
}
}
}
public class BlockChain {
public static List<Block> blockChain = new ArrayList<>();
public static int difficulty = 5;
/**
* 判断整条区块链是否有效
* @return
*/
public static boolean isChainValid() {
Block currentBlock;
Block prevBlock;
String hashTarget = new String(new char[difficulty]).replace('\0','0');
for (int i = 1; i < blockChain.size(); i++) {
currentBlock = blockChain.get(i);
prevBlock = blockChain.get(i - 1);
if (!currentBlock.getHash().equals(currentBlock.calculateHash())) {
System.out.println("当前区块哈希值不匹配");
return false;
}
if (!prevBlock.getHash().equals(currentBlock.getPreHash())) {
System.out.println("前一个区块哈希值不匹配");
return false;
}
if (!currentBlock.getHash().substring(0,difficulty).equals(hashTarget)) {
System.out.println("当前区块有异常");
return false;
}
}
return true;
}
public static void main(String[] args) {
//生成首区块
Block genesisBlock = new Block("first","0");
blockChain.add(genesisBlock);
//挖矿
blockChain.get(0).mineBlock(difficulty);
System.out.println(genesisBlock);
//第二个区块
Block secBlock = new Block("second",genesisBlock.getHash());
blockChain.add(secBlock);
blockChain.get(1).mineBlock(difficulty);
System.out.println(secBlock);
//第三个区块
Block thirdBlock = new Block("third",secBlock.getHash());
blockChain.add(thirdBlock);
blockChain.get(2).mineBlock(difficulty);
System.out.println(thirdBlock);
System.out.println("区块链有效性: " + isChainValid());
System.out.println(JSONObject.toJSONString(blockChain));
}
}
运行结果
Block Mind!!!: 000000dcabbe4bbeccbedc1efc25a886c598652e1efac0dfe1a3d74d9bc9f858
Block(hash=000000dcabbe4bbeccbedc1efc25a886c598652e1efac0dfe1a3d74d9bc9f858, preHash=0, data=first, timeStamp=1608653349150, nonce=1196438)
Block Mind!!!: 00000f6e06be2d09e9e8ba232d84ebe0de75b8e395feaa0f3ddb01377b55c72d
Block(hash=00000f6e06be2d09e9e8ba232d84ebe0de75b8e395feaa0f3ddb01377b55c72d, preHash=000000dcabbe4bbeccbedc1efc25a886c598652e1efac0dfe1a3d74d9bc9f858, data=second, timeStamp=1608653351114, nonce=1312709)
Block Mind!!!: 0000004bdf0b2af602460d79cdd247ac1b57fb651eb68ae68198b514de490569
Block(hash=0000004bdf0b2af602460d79cdd247ac1b57fb651eb68ae68198b514de490569, preHash=00000f6e06be2d09e9e8ba232d84ebe0de75b8e395feaa0f3ddb01377b55c72d, data=third, timeStamp=1608653353097, nonce=1552650)
区块链有效性: true
[{"data":"first","hash":"000000dcabbe4bbeccbedc1efc25a886c598652e1efac0dfe1a3d74d9bc9f858","nonce":1196438,"preHash":"0","timeStamp":1608653349150},{"data":"second","hash":"00000f6e06be2d09e9e8ba232d84ebe0de75b8e395feaa0f3ddb01377b55c72d","nonce":1312709,"preHash":"000000dcabbe4bbeccbedc1efc25a886c598652e1efac0dfe1a3d74d9bc9f858","timeStamp":1608653351114},{"data":"third","hash":"0000004bdf0b2af602460d79cdd247ac1b57fb651eb68ae68198b514de490569","nonce":1552650,"preHash":"00000f6e06be2d09e9e8ba232d84ebe0de75b8e395feaa0f3ddb01377b55c72d","timeStamp":1608653353097}]
本文到此结束,希望对大家有所帮助。
特别声明:本文来自网络或网友投稿,部分图片和文字来源于网络收集整理,仅供学习交流,版权归原作者所有,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。如果您发现网站上有侵犯您的知识产权的作品,请与我们取得联系,我们会及时修改或删除;邮箱:1091218940@qq.com
本页标题:区块链的实现
留言通道
发送成功之后,我们会尽快回复您!
Online Consulting