import java.util.Block;

public class Block public String hash;

public String previousHash;

private String data;
private long timeStamp;

//Block Generator
public
Block (String data, String previousHash)
{

  this.data = data;

  this.previousHash = previousHash;

  this.timeStamp = new Date ().getTime ();

}}

public String
calculateHash ()
{

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

  return calculatedhash;

  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.
}


import java.util.ArrayList;

import com.google.gson.GsonBuilder;

public class Digital_Certificate
{

  public static ArrayList < Block > blockchain = new ArrayList < Block > ();

  public static void main (String[]args)
  {

    blockchain.add (new Block ("User Interface agent", "0"));

    blockchain.add (new
		    Block ("User Interface agent ",
			   blockchain.get (blockchain.size () - 1).hash));

    blockchain.add (new
		    Block ("User Interface agent ",
			   blockchain.get (blockchain.size () - 1).hash));

    String blockchainJson =
      new GsonBuilder ().setPrettyPrinting ().create ().toJson (blockchain);

      System.out.println (blockchainJson);

}}
public static Boolean
isChainValid ()
{

  Block username;

  Block password;

//loop to check the username:
  for (int i = 1; i < blockchain.size (); i++)

    {

      Username = blockchain.get (i[]);

      Password = blockchain.get (j[]);

//compare username and password:
      if (!currentBlock.hash.equals (currentBlock.calculateHash ()))

	{

	  System.out.println ("Enter the valid username ");

	  return false;

	}

// loop to check the password 
      if (!previousBlock.hash.equals (currentBlock.previousHash))

	{

	  System.out.println ("Password is invalid");

	  return false;

	}

    }

  return true;

}


import java.util.Date;

public class Block
{

  public String hash;

  public String previousHash;

  private String data;

  private long timeStamp;

  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 ();

  }
  public String calculateHash ()
  {

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

    return calculatedhash;

  }

  public void mineBlock (int user verification)
  {

    String target = new String (new char[user verification]).replace ('\0', '0');	//Create a string with user verification * "0"
    while (!hash.substring (0, user verification).equals (target))
      {

	nonce++;

	hash = calculateHash ();

      }

    System.out.println ("Block Mined!!! : " + hash);

  }

}


import java.util.ArrayList;

import com.google.gson.GsonBuilder;

public class Digital_Certificate
{

  public static ArrayList < Block > blockchain = new ArrayList < Block > ();

  public static int userverification = 1;

  public static void main (String[]args)
  {

//Cfreate the digital certificate based on UA agent:
    blockchain.add (new Block ("Digital Certificate", "0"));

    System.out.println ("Trying obtain Digital Certificate... ");

    blockchain.get (0).mineBlock (userverification);

    blockchain.add (new
		    Block ("User is verified",
			   blockchain.get (blockchain.size () - 1).hash));

    System.out.println ("Trying obtain Digital Certificate... ");

    blockchain.get (1).mineBlock (userverification);

    blockchain.add (new
		    Block ("Issue the digital certificate",
			   blockchain.get (blockchain.size () - 1).hash));

    System.out.println ("User is verified... ");

    blockchain.get (2).mineBlock (userverification);

    System.out.println ("\nBlockchain is Valid: " + isChainValid ());

    String blockchainJson = new
      GsonBuilder ().setPrettyPrinting ().create ().toJson (blockchain);

      System.out.println ("\nThe block chain: ");

      System.out.println (blockchainJson);

  }
  public static Boolean isChainValid ()
  {

    Block currentBlock;

    Block previousBlock;

    String hashTarget =
      new String (new char[user verification]).replace ('\0', '0');

//loop through blockchain to check hashes:
    for (int i = 1; i < blockchain.size (); i++)
      {

	currentBlock = blockchain.get (i);

	previousBlock = blockchain.get (i - 1);

//compare registered hash and calculated hash:
	if (!currentBlock.hash.equals (currentBlock.calculateHash ()))
	  {

	    System.out.println ("Current Hashes not equal");

	    return false;

	  }

//compare previous hash and registered previous hash
	if (!previousBlock.hash.equals (currentBlock.previousHash))
	  {

	    System.out.println ("Previous Hashes not equal");

	    return false;

	  }

//check if hash is solved
	if (!currentBlock.hash.
	    substring (0, user verification).equals (hashTarget))
	  {

	    System.out.println ("Invalid User");

	    return false;

	  }

      }

    return true;

  }

}
