Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/blockchain/Block.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package blockchain;

public class Block {

private final Integer id;
private final String hashPreviousBlock;
private final String hash;
private final Long timestamp;


public Block(Integer id, String hashPreviousBlock, String hash, Long timestamp) {
this.id = id;
this.hashPreviousBlock = hashPreviousBlock;
this.hash = hash;
this.timestamp = timestamp;
}

public Integer getId() {
return id;
}

public String getHashPreviousBlock() {
return hashPreviousBlock;
}

public String getHash() {
return hash;
}

public Long getTimestamp() {
return timestamp;
}
}
43 changes: 43 additions & 0 deletions src/blockchain/Blockchain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package blockchain;

import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;

public class Blockchain implements Iterable<Block> {

private final LinkedList<Block> blocks;

public Blockchain() {
blocks = new LinkedList<>(Collections.singleton(
new Block(1,
"0",
StringUtil.applySha256("0"),
new Date().getTime()))
);
}

public void generate() {
Long timestamp = new Date().getTime();
Block tailBlock = blocks.getLast();
String hash = StringUtil.applySha256(tailBlock.getHash());
blocks.add(new Block(tailBlock.getId() + 1, tailBlock.getHash(), hash, timestamp));
}

public boolean validate() {
for (int i = 1; i < blocks.size(); i++) {
Block prev = blocks.get(i - 1);
Block cur = blocks.get(i);
if (!cur.getHashPreviousBlock().equals(prev.getHash())) {
return false;
}
}
return true;
}

@Override
public Iterator<Block> iterator() {
return blocks.iterator();
}
}
16 changes: 14 additions & 2 deletions src/blockchain/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package blockchain;

public class Main {
public static void main(String[] args) {
System.out.print("Hello world!");

public static void main(String[] args) {
Blockchain blockchain = new Blockchain();
for (int i = 0; i < 10; i++) {
blockchain.generate();
}

for (Block block : blockchain) {
System.out.printf("Block:\n");
System.out.printf("Id: %s\n", block.getId());
System.out.printf("Timestamp: %s\n", block.getTimestamp());
System.out.printf("Hash of the previous block:\n%s\n", block.getHashPreviousBlock());
System.out.printf("Hash of the block: \n%s\n\n", block.getHash());
}
}
}
28 changes: 28 additions & 0 deletions src/blockchain/StringUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package blockchain;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

public class StringUtil {

/**
* Applies Sha256 to a string and returns a hash.
*/
public static String applySha256(String input) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(input.getBytes(StandardCharsets.UTF_8));
StringBuilder hexString = new StringBuilder();
for (byte elem : hash) {
String hex = Integer.toHexString(0xff & elem);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}