com.wanmait.chian.data.Block.java
package com.wanmait.chain.data; import com.wanmait.chain.util.Const; import com.wanmait.chain.util.SignatureUtil; /** * 区块定义 * @author 万码学堂 * */ public class Block { public String sign;//本区块的hash值(数字签名) public String prevSign;//前一区块的hash值(数字签名) private String data;//本区块保存的数据 private long time;//时间戳 private int nonce = 0; public Block(String data,String prevSign) { this.data = data; this.prevSign = prevSign; this.time = System.currentTimeMillis(); this.sign = this.createHash(); mineBlock(Const.DIFFICULTY); } public String createHash() { //产生的数字签名和前一个块的签名和时间和数据都有关系,这样做到防止篡改 return SignatureUtil.createSign(prevSign+time+nonce+data); } public void mineBlock(int difficulty) { String target = new String(new char[difficulty]).replace('\0', '0'); //Create a string with difficulty * "0" while(!sign.substring( 0, difficulty).equals(target)) { nonce ++; sign = createHash(); } } @Override public String toString() { return "{" +"sign:'"+sign+"',\n" +"prevSign:'"+prevSign+"',\n" +"data:'"+data+"',\n" +"time:'"+time+"',\n" +"nonce:"+nonce+"" + "}"; } }
com.wanmait.chain.data.BlockChain.java
package com.wanmait.chain.data; import java.util.ArrayList; import java.util.List; import com.wanmait.chain.util.Const; public class BlockChain { public static boolean chainValid(List blocks) { if(blocks==null||blocks.size()<=1) return false; String hashTarget = new String(new char[Const.DIFFICULTY]).replace('\0', '0'); Block currentBlock; Block prevBlock; for(int i=1;i<blocks.size();i++) { currentBlock = blocks.get(i); prevBlock = blocks.get(i-1); if(currentBlock==null||currentBlock.sign==null||prevBlock==null||prevBlock.sign==null) return false; if(!currentBlock.sign.equals(currentBlock.createHash())) { return false; } if(!prevBlock.sign.equals(currentBlock.prevSign)) { return false; } if(!currentBlock.sign.substring(0,Const.DIFFICULTY).equals(hashTarget)) { return false; } } return true; } public static String getJson(List blocks) { StringBuilder buf = new StringBuilder(); buf.append("["); for(Block block:blocks) { buf.append(block); if(block!=blocks.get(blocks.size()-1)) { buf.append(",\n"); } } buf.append("]"); return buf.toString(); } public static List createChain(String...datas){ ArrayList blocks = new ArrayList<>(); blocks.add(new Block(null, "0"));//创世块,未存放数据 for(String data:datas) { blocks.add(new Block(data, blocks.get(blocks.size()-1).sign)); } return blocks; } }
com.wanmait.chain.util.Const.java
package com.wanmait.chain.util; public class Const { public static final int DIFFICULTY = 5; }
com.wanmait.chain.util.SignatureUtil.java
package com.wanmait.chain.util; import java.security.MessageDigest; /** * 数字签名产生工具类 * @author 万码学堂 * */ public class SignatureUtil { /** * 创建一个签名,使用的SHA256算法 * @param input * @return */ public static String createSign(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(); 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); } } }
最后测试类:
com.wanmait.chain.test.Test.java
package com.wanmait.chain.test; import java.util.List; import com.wanmait.chain.data.Block; import com.wanmait.chain.data.BlockChain; public class Test { public static void main(String[] args) { List blocks = BlockChain.createChain("万码学堂","wanmait.net","做最负责任的教育"); String json = BlockChain.getJson(blocks); System.out.println(json); } }
0条评论
点击登录参与评论