接着上一篇.
上一篇還沒有輸出,其實加入了index在實體類中.
package com.sha256.sha256.bean;
import com.sha256.sha256.utils.SHA256Util;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Block {
private long index;
private String hash; // our signature
private String previousHash; // the hash of previous block
private String data; //our data will be a simple message.
private long timeStamp; //as number of milliseconds since 1/1/1970.
//Block Constructor
public Block(long index,String data,String previousHash){
this.index = index;
this.data = data;
this.previousHash = previousHash;
this.timeStamp = new Date().getTime();
this.hash = SHA256Util.calculateHash(this); //Making sure we do this after we set the other values.
}
}
測試類:
package com.sha256.sha256.test;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.GsonBuilder;
import com.sha256.sha256.bean.Block;
import com.sha256.sha256.utils.SHA256Util;
import java.util.ArrayList;
import java.util.Random;
public class TestSHA256 {
//聲明一個區塊鏈,用於添加Block實體
public static ArrayList<Block> blockChain = new ArrayList<>();
public static void main(String[] args) {
//test1 測試三個被加密字符串 加密後的hash值的差別
/**
* 雖然第三條信息僅僅多一個".",但加密後的數據hash相差極大
*/
// String message0 = "我是要被加密的信息";
// String message1 = "我是要被加密的信息";
// String message2 = "我是要被加密的信息.";
// String encryptionMessage0 = SHA256Util.applySha256(message0);
// String encryptionMessage1 = SHA256Util.applySha256(message1);
// String encryptionMessage2 = SHA256Util.applySha256(message2);
// System.out.println(encryptionMessage0);
// System.out.println(encryptionMessage1);
// System.out.println(encryptionMessage2);
//
// //test2 創建區塊鏈邏輯, 因爲第一個塊沒有上一個塊的hash頭部值,所以輸入0 作爲前一個塊的previous hash
// /**
// * 由於在{@link SHA256Util#calculateHash(Block)}
// * 中對同時產生的new Date().getTime() (timestamp)
// * 也加入進行了hash加密,所以固有的message (data)及
// * previoushash之和進行了加密.
// */
// Block genesisBlock = new Block(0,"這是第一個區塊中的要被加密的信息和交易信息","0");
// String hash1 = genesisBlock.getHash();
// System.out.println("Hash for block 1 : "+hash1);
//
// Block secondBlock = new Block(1,"這是第二個區塊,以及其中信息!!!它的前區塊頭部hash我們拿上一個的來使用",hash1);
// String hash2 = secondBlock.getHash(); //
// System.out.println("Hash for block 2 : "+hash2);
//
// Block thirdBlock = new Block(2,"這是第三個區塊,它的hash應該已經被前兩個的信息納入進來了,它的hash如果對不上,那麼說明前面的信息被改動過了",hash2);
// String hash3 = thirdBlock.getHash();
// System.out.println("Hash for block 3 : "+hash3);
//
//// test3 add our blocks to the blockchain ArrayList :
// blockChain.add(new Block(0,"區塊鏈上第一小節","0"));
// blockChain.add(new Block(1,"區塊鏈第二小節",blockChain.get(blockChain.size()-1).getHash()));
// blockChain.add(new Block(2,"區塊鏈第三小節",blockChain.get(blockChain.size()-1).getHash()));
int chainNumber = 24;
int index = 0;
while (chainNumber > 0) {
System.out.println("blockChain.size():" + blockChain.size());
if (blockChain.size() == 0) {
blockChain.add(new Block(0, "創世塊", "0"));
}
index++;
blockChain.add(new Block(index, "區塊內容" + blockChainMessage(index), blockChain.get(blockChain.size() - 1).getHash()));
chainNumber--;
}
// JSONArray blockChainJson1 = (JSONArray)JSONArray.toJSON(blockChain); //JSONArray是不排版的
// System.out.println(blockChainJson1);
String blockChainJson = new GsonBuilder().setPrettyPrinting().create().toJson(blockChain);
System.out.println(blockChainJson);
}
//模擬一些交易信息
private static String blockChainMessage(int getNumber) {
Random random = new Random(getNumber);
long l = random.nextLong();
System.out.println("blockChainMessage:" + l);
return String.valueOf(l);
}
}