Skip to content

Instantly share code, notes, and snippets.

@DrBrad
Last active May 27, 2020 02:25
Show Gist options
  • Save DrBrad/c2727691aab75e14e9b69edf87111b96 to your computer and use it in GitHub Desktop.
Save DrBrad/c2727691aab75e14e9b69edf87111b96 to your computer and use it in GitHub Desktop.
Quick and Easy BlockChain
import java.security.MessageDigest;
import java.util.Date;
import static *.BlockHandler.*;
import static java.nio.charset.StandardCharsets.UTF_8;
public class Block {
public String hash;
public String previousHash;
public String data;
public long timeStamp;
public int nonce;
public Block(String data, String previousHash){
this.data = data;
this.previousHash = previousHash;
this.timeStamp = new Date().getTime();
this.hash = calculateBlockHash();
}
public Block(String data, String previousHash, String hash, long timeStamp, int nonce){
this.data = data;
this.previousHash = previousHash;
this.hash = hash;
this.timeStamp = timeStamp;
this.nonce = nonce;
}
public String calculateBlockHash(){
String dataToHash = previousHash+timeStamp+nonce+data;
byte[] bytes;
try{
MessageDigest digest = MessageDigest.getInstance("SHA-512");
bytes = digest.digest(dataToHash.getBytes(UTF_8));
StringBuffer buffer = new StringBuffer();
for(byte b : bytes){
buffer.append(String.format("%02x", b));
}
return buffer.toString();
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public String mineBlock(){
String prefixString = new String(new char[difficulty]).replace('\0', '0');
while(!hash.substring(0, difficulty).equals(prefixString)){
nonce++;
hash = calculateBlockHash();
}
System.out.println("Block Mined!!! : " + hash);
return hash;
}
}
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class BlockHandler {
public static int difficulty = 2;
public static JSONObject blockToJSON(Block block){
JSONObject jblock = new JSONObject();
jblock.put("p", block.previousHash);
jblock.put("d", block.data);
jblock.put("t", block.timeStamp);
jblock.put("n", block.nonce);
jblock.put("h", block.hash);
return jblock;
}
public static JSONArray blocksToJSON(ArrayList<Block> blocks){
JSONArray jblocks = new JSONArray();
for(Block block : blocks){
JSONObject jblock = new JSONObject();
jblock.put("p", block.previousHash);
jblock.put("d", block.data);
jblock.put("t", block.timeStamp);
jblock.put("n", block.nonce);
jblock.put("h", block.hash);
jblocks.put(jblock);
}
return jblocks;
}
public static Block stringToBlock(String text){
JSONObject json = new JSONObject(text);
Block block = new Block(json.getString("d"),
json.getString("p"),
json.getString("h"),
json.getLong("t"),
json.getInt("n"));
return block;
}
public static ArrayList<Block> stringToBlocks(String text){
JSONArray json = new JSONArray(text);
ArrayList<Block> blocks = new ArrayList<>();
for(int i = 0; i < json.length(); i++){
JSONObject jblock = json.getJSONObject(i);
Block block = new Block(jblock.getString("d"),
jblock.getString("p"),
jblock.getString("h"),
jblock.getLong("t"),
jblock.getInt("n"));
blocks.add(block);
}
return blocks;
}
public static Boolean isBlockValid(Block block){
String hashTarget = new String(new char[difficulty]).replace('\0', '0');
//compare registered hash and calculated hash:
if(!block.hash.equals(block.calculateBlockHash())){
//System.out.println("Current Hashes not equal");
return false;
}
//check if hash is solved
Matcher matcher = Pattern.compile("(.*?)[a-f1-9].*").matcher(block.hash);
if(matcher.matches()){
if(!matcher.group(1).equals(hashTarget)){
return false;
}
}else{
return false;
}
return true;
}
public static Boolean isChainValid(ArrayList<Block> blocks){
Block currentBlock;
Block previousBlock;
String hashTarget = new String(new char[difficulty]).replace('\0', '0');
//loop through blockchain to check hashes:
for(int i = 1; i < blocks.size(); i++){
currentBlock = blocks.get(i);
previousBlock = blocks.get(i-1);
//compare registered hash and calculated hash:
if(!currentBlock.hash.equals(currentBlock.calculateBlockHash())){
//System.out.println("Current Hashes not equal");
return false;
}
//compare previous hash and registered previous hash
if(!previousBlock.hash.equals(currentBlock.previousHash)){
//System.out.println("Previous Hashes not equal");
return false;
}
//check if hash is solved
Matcher matcher = Pattern.compile("(.*?)[a-f1-9].*").matcher(currentBlock.hash);
if(matcher.matches()){
if(!matcher.group(1).equals(hashTarget)){
return false;
}
}else{
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment