From d589f801c248f7cedcc42ae4c80d587dc26f1890 Mon Sep 17 00:00:00 2001 From: aviperl Date: Mon, 28 Jun 2021 12:00:21 -0400 Subject: [PATCH] Cleanup code --- src/noobchain/Block.java | 73 +++++++++++++++++------------------ src/noobchain/StringUtil.java | 15 +++---- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/noobchain/Block.java b/src/noobchain/Block.java index f8b63b6..3a34e13 100644 --- a/src/noobchain/Block.java +++ b/src/noobchain/Block.java @@ -3,41 +3,40 @@ import java.util.Date; public class Block { - - public String hash; - public String previousHash; - private String data; //our data will be a simple message. - private long timeStamp; //as number of milliseconds since 1/1/1970. - private int nonce; - - //Block Constructor. - public Block(String data,String previousHash ) { - this.data = data; - this.previousHash = previousHash; - this.timeStamp = new Date().getTime(); - - this.hash = calculateHash(); //Making sure we do this after we set the other values. - } - - //Calculate new hash based on blocks contents - public String calculateHash() { - String calculatedhash = StringUtil.applySha256( - previousHash + - Long.toString(timeStamp) + - Integer.toString(nonce) + - data - ); - return calculatedhash; - } - - //Increases nonce value until hash target is reached. - public void mineBlock(int difficulty) { - String target = StringUtil.getDificultyString(difficulty); //Create a string with difficulty * "0" - while(!hash.substring( 0, difficulty).equals(target)) { - nonce ++; - hash = calculateHash(); - } - System.out.println("Block Mined!!! : " + hash); - } - + + public String hash; + public String previousHash; + private final String data; //our data will be a simple message. + private final long timeStamp; //as number of milliseconds since 1/1/1970. + private int nonce; + + //Block Constructor. + public Block(String data, String previousHash) { + this.data = data; + this.previousHash = previousHash; + this.timeStamp = new Date().getTime(); + + this.hash = calculateHash(); //Making sure we do this after we set the other values. + } + + //Calculate new hash based on blocks contents + public String calculateHash() { + return StringUtil.applySha256( + previousHash + + timeStamp + + nonce + + data + ); + } + + //Increases nonce value until hash target is reached. + public void mineBlock(int difficulty) { + String target = StringUtil.getDifficultyString(difficulty); //Create a string with difficulty * "0" + while (!hash.substring(0, difficulty).equals(target)) { + nonce++; + hash = calculateHash(); + } + System.out.println("Block Mined!!! : " + hash); + } + } diff --git a/src/noobchain/StringUtil.java b/src/noobchain/StringUtil.java index d0a8ecb..3385f3e 100644 --- a/src/noobchain/StringUtil.java +++ b/src/noobchain/StringUtil.java @@ -1,4 +1,5 @@ package noobchain; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import com.google.gson.GsonBuilder; @@ -12,12 +13,12 @@ public static String applySha256(String input){ MessageDigest digest = MessageDigest.getInstance("SHA-256"); //Applies sha256 to our input, - byte[] hash = digest.digest(input.getBytes("UTF-8")); + byte[] hash = digest.digest(input.getBytes(StandardCharsets.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'); + StringBuilder hexString = new StringBuilder(); // This will contain hash as hexidecimal + for (byte b : hash) { + String hex = Integer.toHexString(0xff & b); + if (hex.length() == 1) hexString.append('0'); hexString.append(hex); } return hexString.toString(); @@ -33,8 +34,8 @@ public static String getJson(Object o) { } //Returns difficulty string target, to compare to hash. eg difficulty of 5 will return "00000" - public static String getDificultyString(int difficulty) { - return new String(new char[difficulty]).replace('\0', '0'); + public static String getDifficultyString(int difficulty) { + return "0".repeat(difficulty); }