Last active
December 12, 2021 15:19
-
-
Save annhienktuit/7eacbe3ea59a1000d1795c44a295cf22 to your computer and use it in GitHub Desktop.
A sample blockchain written in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.MessageDigest; | |
import java.util.*; | |
class Block { | |
private String hash; | |
private String previousHash; | |
private String data; | |
private long timeStamp; | |
private int nonce; | |
public Block(String data, String previousHash, long timeStamp){ | |
this.data = data; | |
this.previousHash = previousHash; | |
this.timeStamp = timeStamp; | |
this.hash = calculateBlockHash(); | |
} | |
public String getHash() { | |
return this.hash; | |
} | |
public void setHash(String hash) { | |
this.hash = hash; | |
} | |
public String getPreviousHash() { | |
return this.previousHash; | |
} | |
public void setPreviousHash(String previousHash) { | |
this.previousHash = previousHash; | |
} | |
public String getData() { | |
return this.data; | |
} | |
public void setData(String data) { | |
this.data = data; | |
} | |
public long getTimeStamp() { | |
return this.timeStamp; | |
} | |
public void setTimeStamp(long timeStamp) { | |
this.timeStamp = timeStamp; | |
} | |
public int getNonce() { | |
return this.nonce; | |
} | |
public void setNonce(int nonce) { | |
this.nonce = nonce; | |
} | |
public String calculateBlockHash() { | |
String dataToHash = this.previousHash + this.data + Long.toString(timeStamp) + Integer.toString(nonce); | |
MessageDigest digest = null; | |
byte[] bytes = null; | |
try{ | |
digest = MessageDigest.getInstance("SHA-256"); | |
bytes = digest.digest(dataToHash.getBytes("UTF-8")); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
StringBuffer buffer = new StringBuffer(); | |
for(byte b : bytes) { | |
buffer.append(String.format("%02x", b)); | |
} | |
return buffer.toString(); | |
} | |
public String mineBlock(int prefix){ | |
String prefixString = new String(new char[prefix]).replace('\0', '0'); | |
while(!this.hash.substring(0, prefix).equals(prefixString)){ | |
this.nonce++; | |
this.hash = calculateBlockHash(); | |
} | |
return hash; | |
} | |
} | |
public class SampleChain{ | |
public static List<Block> blockChain = new ArrayList<>(); | |
static int prefix = 4; | |
static String prefixString = new String(new char[prefix]).replace('\0', '0'); | |
public static void main(String []args){ | |
Block genesisBlock = new Block("Genesis","0", new Date().getTime()); | |
blockChain.add(genesisBlock); | |
Block newBlock = new Block("Second block", blockChain.get(blockChain.size() - 1).getHash(),new Date().getTime()); | |
newBlock.mineBlock(prefix); | |
System.out.println(newBlock.getHash().substring(0, prefix).equals(prefixString)); | |
blockChain.add(newBlock); | |
for(Block block: blockChain){ | |
System.out.println(block.getHash()); | |
System.out.println(block.getNonce()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment