DEV Community

Discussion on: Trying to understand blockchain by making one!

Collapse
 
pratikaambani profile image
Pratik Ambani

Would anybody like to volunteer to write the same in Java?

Collapse
 
maciejprzerwa profile image
Maciej Przerwa • Edited

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class BlockchainExample {

//list of hash values
List<String> blocks = new ArrayList<String>();

public void initBlockchain() {
    String msg = "Hello world";
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
    String previousHash = "0";
    String index = "0";

    hashBlock(msg, timeStamp, previousHash, index);
}

public void hashBlock(String msg, String timestamp, String prevHash, String index) {

    String hash = "";
    Integer nonce = 0;

    while(!isHashValid(hash)) {
        String input = msg+timestamp+prevHash+index+nonce.toString();
        hash = sha256(input);
        nonce+=1;
    }
    System.out.println(nonce.toString());
    blocks.add(hash);   
}

public String getLastHash() {
    return blocks.get(blocks.size()-1);
}

public boolean isHashValid(String hash) {
    if (hash.startsWith("0000")) return true;
    else return false;
}

public void addNewBlock(String msg){
    Integer in = blocks.size();
    String previousHash = getLastHash();
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());

    hashBlock(msg, timeStamp, previousHash, in.toString());
}


public void getAllBlocks() {
    for(int i=0;i<blocks.size();i++){
        System.out.println(blocks.get(i));
    } 
}


public String sha256(String password) {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    md.update(password.getBytes());

    byte byteData[] = md.digest();

    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < byteData.length; i++) {
     sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
    }

    return sb.toString();
}


public static void main(String[] args) {

    BlockchainExample be = new BlockchainExample();

    be.initBlockchain();
    be.addNewBlock("First new block");
    be.addNewBlock("Second new block");

    be.getAllBlocks();
}

}

Collapse
 
eric7giants profile image
Eric7Giants

New to Java and I was referencing this for a Security course. I'm getting BlockchainExample cannot be resolved to a type.

Thread Thread
 
maciejprzerwa profile image
Maciej Przerwa

Hi there, could you specify in which line you are getting this error?
And also, did you copy all the code? Not only part on black? There are lines at the top

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class BlockchainExample {

and also one
}
at the end.
I have checked it minute ago - code works.