Skip to content

Instantly share code, notes, and snippets.

@jsphdnl
Created January 9, 2018 12:54
Show Gist options
  • Save jsphdnl/7d5fe4d389ff5bb9d6831215ed900325 to your computer and use it in GitHub Desktop.
Save jsphdnl/7d5fe4d389ff5bb9d6831215ed900325 to your computer and use it in GitHub Desktop.
BlockChain from Scratch Part 01 - java
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* Compile the class with jackson-data bind dependency
* <p>com.fasterxml.jackson.core<p/>
*/
class TRBlock {
private static final String SHA1 = "SHA-1";
private final static char[] hexArray = "0123456789abcdef".toCharArray();
public int index; // The index of the block
public long timestamp; // Time stamp in epoch
public Object data; // The data we want to store on the Block chain
public String prevHash; // This is the chain/hash previous block
public String nonce; // A Magical number that needs to be mined/found
public int target; // Number of leading zeros in the current hash
@JsonIgnore
public String currHash; // Current hash
/**
* Constructor for creating the block
*
* @param index the index of the block
* @param timestamp the time stamp in epoch
* @param data data to be stored in the block chain
* @param prevHash hash of the previous block
* @param nonce the nonce to be mined
* @param target the number of leading zeros to mine the nonce
*/
public TRBlock(int index, long timestamp, Object data, String prevHash,
String nonce, int target) {
this.index = index;
this.timestamp = timestamp;
this.data = data;
this.prevHash = prevHash;
this.nonce = nonce;
this.target = target;
}
/**
* Convert a byte array into hexadecimal string
* @param bytes data in bytes array
* @return a string in hexadecimal
*/
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
/**
* Generate SHA1 hash from a text
* @param data the text data to be hashed
* @return a string in base64 encoded format
* @throws java.security.NoSuchAlgorithmException
*/
public static String generateSHA1Hash(String data) throws NoSuchAlgorithmException {
MessageDigest messageDigest = MessageDigest.getInstance(SHA1);
messageDigest.update(data.getBytes());
byte [] hash = messageDigest.digest();
messageDigest.reset();
return bytesToHex(hash);
}
/**
* Convert TRBlock into json string
* @param obj object to be stringyfied
* @return json string of the object
* @throws JsonProcessingException
*/
public static String stringify(Object obj) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(obj);
}
public static void main(String[] args) throws JsonProcessingException,
UnsupportedEncodingException, NoSuchAlgorithmException {
TRBlock block = new TRBlock(0, 1514528022, "Hello World",
"000000000000000000000000000", "Random nonce", 8);
System.out.println(generateSHA1Hash(stringify(block)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment