diff --git a/M_Layashree_Assignment/Block.class b/M_Layashree_Assignment/Block.class new file mode 100644 index 0000000..6558f62 Binary files /dev/null and b/M_Layashree_Assignment/Block.class differ diff --git a/M_Layashree_Assignment/Blockchain.class b/M_Layashree_Assignment/Blockchain.class new file mode 100644 index 0000000..1c5fd38 Binary files /dev/null and b/M_Layashree_Assignment/Blockchain.class differ diff --git a/M_Layashree_Assignment/HugoByte Full Code.txt b/M_Layashree_Assignment/HugoByte Full Code.txt new file mode 100644 index 0000000..e9aeb5b --- /dev/null +++ b/M_Layashree_Assignment/HugoByte Full Code.txt @@ -0,0 +1,132 @@ +import java.util.List; +import java.util.LinkedList; +import java.security.NoSuchAlgorithmException; +import java.security.MessageDigest; + +class Block +{ + private List transactions; + private String previousHash; + private String hash; + + public Block(List transactions, String previousHash) + { + this.transactions = transactions; + this.previousHash = previousHash; + this.hash = computeHash(); + } + + public String computeHash() + { + try + { + MessageDigest md = MessageDigest.getInstance("SHA-256"); + String data = transactions.toString() + previousHash; + byte[] hashBytes = md.digest(data.getBytes()); + StringBuilder hexString = new StringBuilder(); + + for (byte hashByte : hashBytes) + { + String hexDecimal = Integer.toHexString(0xff & hashByte); + if (hexDecimal.length() == 1) + { + hexString.append('0'); + } + hexString.append(hexDecimal); + } + return hexString.toString(); + } + catch (NoSuchAlgorithmException e) + { + e.printStackTrace(); + return null; + } + } + + public String getHash() + { + return hash; + } + + public String getPreviousHash() + { + return previousHash; + } + + public List getTransactions() + { + return transactions; + } +} + +class Blockchain +{ + private List chain; + + public Blockchain() // Executed when a new Blockchain object is created. + { + chain = new LinkedList<>(); + chain.add(createGenesisBlock()); + /* The createGenesisBlock() method is called to generate the first block (also known as the "genesis block"), which contains an empty list of transactions and a previous hash value of "0". This block is then added to the chain. */ + } + + private Block createGenesisBlock() //Create and return the genesis block. This block serves as the starting point of the blockchain. + { + return new Block(new LinkedList<>(), "0"); + } + + // Adds a new block to the chain. It takes a list of transactions as input. + public void addBlock(List transactions) + { + String previousHash = chain.get(chain.size() - 1).getHash(); // Retrieves the hash of the last block in the chain. + Block newBlock = new Block(transactions, previousHash); + chain.add(newBlock); // The new block is added to the chain. + //System.out.println(newBlock.getHash()); + } + + public boolean validate() + { + for (int i = 1; i < chain.size(); i++) + { + Block currentBlock = chain.get(i); + Block previousBlock = chain.get(i - 1); + + if (!currentBlock.getHash().equals(currentBlock.computeHash())) + { + System.out.println("Sorry! Block hash does not match its content."); + return false; + } + + if (!currentBlock.getPreviousHash().equals(previousBlock.getHash())) + { + System.out.println("Oops! Chain is Broken."); + return false; + } + } + System.out.println("Great! Blockchain is Valid."); + return true; + } +} + +public class HugoByte +{ + public static void main(String[] args) + { + // Create a blockchain + Blockchain b = new Blockchain(); + + // Add some transactions to test the chain + List transactions1 = new LinkedList<>(); + transactions1.add("Alice sent Rs 100 as her savings to Bob."); + transactions1.add("Bob invested Alice's Rs 100 in Mutual Funds."); + b.addBlock(transactions1); + + List transactions2 = new LinkedList<>(); + transactions2.add("Bob got Rs 150 back as Returns from Mutual Funds."); + transactions2.add("Bob returned Rs 100 back to Alice retaining the profit he got."); + b.addBlock(transactions2); + + // Validate the blockchain + b.validate(); + } +} \ No newline at end of file diff --git a/M_Layashree_Assignment/HugoByte.class b/M_Layashree_Assignment/HugoByte.class new file mode 100644 index 0000000..a809784 Binary files /dev/null and b/M_Layashree_Assignment/HugoByte.class differ diff --git a/M_Layashree_Assignment/HugoByte.java b/M_Layashree_Assignment/HugoByte.java new file mode 100644 index 0000000..e9aeb5b --- /dev/null +++ b/M_Layashree_Assignment/HugoByte.java @@ -0,0 +1,132 @@ +import java.util.List; +import java.util.LinkedList; +import java.security.NoSuchAlgorithmException; +import java.security.MessageDigest; + +class Block +{ + private List transactions; + private String previousHash; + private String hash; + + public Block(List transactions, String previousHash) + { + this.transactions = transactions; + this.previousHash = previousHash; + this.hash = computeHash(); + } + + public String computeHash() + { + try + { + MessageDigest md = MessageDigest.getInstance("SHA-256"); + String data = transactions.toString() + previousHash; + byte[] hashBytes = md.digest(data.getBytes()); + StringBuilder hexString = new StringBuilder(); + + for (byte hashByte : hashBytes) + { + String hexDecimal = Integer.toHexString(0xff & hashByte); + if (hexDecimal.length() == 1) + { + hexString.append('0'); + } + hexString.append(hexDecimal); + } + return hexString.toString(); + } + catch (NoSuchAlgorithmException e) + { + e.printStackTrace(); + return null; + } + } + + public String getHash() + { + return hash; + } + + public String getPreviousHash() + { + return previousHash; + } + + public List getTransactions() + { + return transactions; + } +} + +class Blockchain +{ + private List chain; + + public Blockchain() // Executed when a new Blockchain object is created. + { + chain = new LinkedList<>(); + chain.add(createGenesisBlock()); + /* The createGenesisBlock() method is called to generate the first block (also known as the "genesis block"), which contains an empty list of transactions and a previous hash value of "0". This block is then added to the chain. */ + } + + private Block createGenesisBlock() //Create and return the genesis block. This block serves as the starting point of the blockchain. + { + return new Block(new LinkedList<>(), "0"); + } + + // Adds a new block to the chain. It takes a list of transactions as input. + public void addBlock(List transactions) + { + String previousHash = chain.get(chain.size() - 1).getHash(); // Retrieves the hash of the last block in the chain. + Block newBlock = new Block(transactions, previousHash); + chain.add(newBlock); // The new block is added to the chain. + //System.out.println(newBlock.getHash()); + } + + public boolean validate() + { + for (int i = 1; i < chain.size(); i++) + { + Block currentBlock = chain.get(i); + Block previousBlock = chain.get(i - 1); + + if (!currentBlock.getHash().equals(currentBlock.computeHash())) + { + System.out.println("Sorry! Block hash does not match its content."); + return false; + } + + if (!currentBlock.getPreviousHash().equals(previousBlock.getHash())) + { + System.out.println("Oops! Chain is Broken."); + return false; + } + } + System.out.println("Great! Blockchain is Valid."); + return true; + } +} + +public class HugoByte +{ + public static void main(String[] args) + { + // Create a blockchain + Blockchain b = new Blockchain(); + + // Add some transactions to test the chain + List transactions1 = new LinkedList<>(); + transactions1.add("Alice sent Rs 100 as her savings to Bob."); + transactions1.add("Bob invested Alice's Rs 100 in Mutual Funds."); + b.addBlock(transactions1); + + List transactions2 = new LinkedList<>(); + transactions2.add("Bob got Rs 150 back as Returns from Mutual Funds."); + transactions2.add("Bob returned Rs 100 back to Alice retaining the profit he got."); + b.addBlock(transactions2); + + // Validate the blockchain + b.validate(); + } +} \ No newline at end of file diff --git a/M_Layashree_Assignment/M_Layashree Assignment.pdf b/M_Layashree_Assignment/M_Layashree Assignment.pdf new file mode 100644 index 0000000..5bca203 Binary files /dev/null and b/M_Layashree_Assignment/M_Layashree Assignment.pdf differ diff --git a/M_Layashree_Assignment/README.md b/M_Layashree_Assignment/README.md new file mode 100644 index 0000000..350c42e --- /dev/null +++ b/M_Layashree_Assignment/README.md @@ -0,0 +1,17 @@ +Procedure to run the code: + +Ensure that JDK is installed on your local computer. +If JDK is not installed, then Download and Install JDK from the following link: +https://www.oracle.com/in/java/technologies/downloads/ + +Once JDK is installed, Open Command Prompt and enter "java-version". This will display the installed Version number. + +To run the code, navigate to the folder containing the code in the command prompt using "cd " command. + +Once you are in the folder containing the code file, you need to compile and run the code. + +To compile the code, enter "javac HugoByte.java". This will generate the .class files. + +Next, to run the code, enter "java HugoByte". + +This will display the output on your screen.