Code Mid Sem.txt

  • Uploaded by: Kopal Anand
  • 0
  • 0
  • December 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Code Mid Sem.txt as PDF for free.

More details

  • Words: 321
  • Pages: 2
import java.util.Date; import java.security.MessageDigest;

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. //Block Constructor. public Block(String data,String previousHash ) { this.data = data; this.previousHash = previousHash; this.timeStamp = new Date().getTime(); } } public class StringUtil { //Applies Sha256 to a string and returns the result. public static String applySha256(String input){ try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); //Applies sha256 to our input, byte[] hash = digest.digest(input.getBytes("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'); hexString.append(hex); } return hexString.toString(); } catch(Exception e) { throw new RuntimeException(e); } } } public String calculateHash() //use our applySha256 helper, in a new method in the Block class, to calculate the hash. We must calculate the hash from all parts of the block we don�t want to be tampered with. So for our block we will include the previousHash, the data and timeStamp.// {

}

String calculatedhash = StringUtil.applySha256( previousHash + Long.toString(timeStamp) + data ); return calculatedhash;

public Block(String data,String previousHash )//add above method to the Block constructor// { 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. } public class minorChain //The first block is called the genesis block, and because there is no previous block we will just enter �0� as the previous hash.// { public static void main(String[] args) { Block genesisBlock = new Block("Hi im the first block", "0"); System.out.println("Hash for block 1 : " + genesisBlock.hash); Block secondBlock = new Block("Yo im the second block",genesisBlock.hash); System.out.println("Hash for block 2 : " + secondBlock.hash); Block thirdBlock = new Block("Hey im the third block",secondBlock.hash); System.out.println("Hash for block 3 : " + thirdBlock.hash); } }

Related Documents

Code Mid Sem.txt
December 2019 8
Mid
June 2020 24
Mid
November 2019 39
Code
June 2020 11
Code
November 2019 28
Code
July 2020 8

More Documents from ""

Code Mid Sem.txt
December 2019 8
1 & 2
December 2019 31
Telecom 1
December 2019 28
Retailing Vocabulary
December 2019 30