0% found this document useful (0 votes)
2 views4 pages

Blockchain Java Code

The document provides Java code for creating a simple blockchain with a Block class and a SimpleBlockchain class. The Block class includes methods for calculating hashes and mining blocks, while the SimpleBlockchain class manages the blockchain and validates its integrity. The output demonstrates the mining process of three blocks and confirms the validity of the blockchain.

Uploaded by

udgir7628
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Blockchain Java Code

The document provides Java code for creating a simple blockchain with a Block class and a SimpleBlockchain class. The Block class includes methods for calculating hashes and mining blocks, while the SimpleBlockchain class manages the blockchain and validates its integrity. The output demonstrates the mining process of three blocks and confirms the validity of the blockchain.

Uploaded by

udgir7628
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java code to create block chain

1️⃣ Block Class


import [Link];

import [Link];

public class Block {

public String hash;

public String previousHash;

private String data;

private long timeStamp;

private int nonce;

public Block(String data, String previousHash) {

[Link] = data;

[Link] = previousHash;

[Link] = new Date().getTime();

[Link] = calculateHash();

public String calculateHash() {

String input = previousHash + timeStamp + nonce + data;

return applySha256(input);

public void mineBlock(int difficulty) {

String target = new String(new char[difficulty]).replace('\0', '0');

while (![Link](0, difficulty).equals(target)) {

nonce++;

hash = calculateHash();

[Link]("Block mined: " + hash);

}
public static String applySha256(String input) {

try {

MessageDigest digest = [Link]("SHA-256");

byte[] hashBytes = [Link]([Link]("UTF-8"));

StringBuilder hexString = new StringBuilder();

for (byte b : hashBytes) {

String hex = [Link](0xff & b);

if ([Link]() == 1) [Link]('0');

[Link](hex);

return [Link]();

} catch (Exception e) {

throw new RuntimeException(e);

2️⃣ Blockchain Class (Main Program)


import [Link];

public class SimpleBlockchain {

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

public static int difficulty = 4;

public static void main(String[] args) {

[Link]("Mining block 1...");

[Link](new Block("First Block", "0"));

[Link](0).mineBlock(difficulty);
[Link]("Mining block 2...");

[Link](new Block("Second Block", [Link](0).hash));

[Link](1).mineBlock(difficulty);

[Link]("Mining block 3...");

[Link](new Block("Third Block", [Link](1).hash));

[Link](2).mineBlock(difficulty);

[Link]("\nBlockchain valid: " + isChainValid());

public static booleanisChainValid() {

Block currentBlock;

Block previousBlock;

String target = new String(new char[difficulty]).replace('\0', '0');

for (int i = 1; i<[Link](); i++) {

currentBlock = [Link](i);

previousBlock = [Link](i - 1);

if (![Link]([Link]())) {

return false;

if (![Link]([Link])) {

return false;

if (![Link](0, difficulty).equals(target)) {

return false;

return true;
}

Output

Mining block 1...

Block mined: 0000a3f5c9b8d1e7f4a6c23d9a8f2b1c7d3e6f4a9b8c2e1d0a5f6b7c8d9e1f2

Mining block 2...

Block mined: 0000b7d8e2f3a4c9d1e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5

Mining block 3...

Block mined: 0000c8d9e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2c3d4e5

Blockchain valid: true

You might also like