0% found this document useful (0 votes)
13 views3 pages

Block

Uploaded by

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

Block

Uploaded by

aartirathod2015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ET-IV LAB 2024-25 1

EXPRIMENT NUMBER-4

AIM: Create a Block of Blockchain using Java Language.


Objective:
 Blocks are files stored by a blockchain, where transaction data are permanently recorded.
 A block records some or all of the most recent transactions not yet validated by the network.
 Once the data are validated, the block is closed.
 Then, a new block is created for new transactions to be entered into and validated.
Requirements:
 Any Text Editor
 Jdk Updated Version
Theory:
Blocks are data structures within the blockchain database, where transaction data in a
crypto currency blockchain are permanently recorded. A block records some or all of the most
recent transactions not yet validated by the network. Once the data are validated, the block is
closed. Then, a new block is created for new transactions to be entered into and validated.
Blocks are created when miners or block validators successfully validate the encrypted information
in the blockheader, which prompts the creation of a new block.

SOURCE CODE:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.util.Date;
public class Block {
private int index;
private long timestamp;
private String previousHash;
private String hash;
private String data;
private int nonce;
public Block(int index, String previousHash, String data) {
this.index = index;

JCOET, YAVATMAL
ET-IV LAB 2024-25 2

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


this.previousHash = previousHash;
this.data = data;
this.nonce = 0;
this.hash = calculateHash();
}
public String calculateHash() {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
String input = index + timestamp + previousHash + data + nonce;
byte[] hashBytes = digest.digest(input.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte hashByte : hashBytes) {
String hex = Integer.toHexString(0xff &hashByte);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
public void mineBlock(int difficulty) {
String target = new String(new char[difficulty]).replace('\0', '0');
while (!hash.substring(0, difficulty).equals(target)) {
nonce++;
hash = calculateHash();

JCOET, YAVATMAL
ET-IV LAB 2024-25 3

}
System.out.println("Block mined: " + hash);
}
public int getIndex() {
return index;
}
public long getTimestamp() {
return timestamp;
}
public String getPreviousHash() {
return previousHash;
}
public String getHash() {
return hash;
}
public String getData() {
return data;
}
public static void main(String args[]){
Block b=new
Block(1,"3a42c503953909637f78dd8c99b3b85ddde362415585afc11901bdefe8349102","jcoet");
b.calculateHash();
b.mineBlock(1);
b.getIndex();
b.getTimestamp();
b.getPreviousHash();
b.getHash();
b.getData();
}}

JCOET, YAVATMAL

You might also like