Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 36 additions & 37 deletions src/noobchain/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
15 changes: 8 additions & 7 deletions src/noobchain/StringUtil.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package noobchain;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

import com.google.gson.GsonBuilder;
Expand All @@ -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();
Expand All @@ -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);
}


Expand Down