From 23ca111b95640b898b33d63b805b4994eb740101 Mon Sep 17 00:00:00 2001 From: AlinaOktyabrskaya <44409263+AlinaOktyabrskaya@users.noreply.github.com> Date: Fri, 2 Nov 2018 23:47:55 +0300 Subject: [PATCH 1/2] Update Main.java --- src/blockchain/Main.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/blockchain/Main.java b/src/blockchain/Main.java index adf1d02..127549e 100644 --- a/src/blockchain/Main.java +++ b/src/blockchain/Main.java @@ -2,6 +2,6 @@ public class Main { public static void main(String[] args) { - System.out.print("Hello world!"); + new Blockchain(); } -} \ No newline at end of file +} From 8fac7564ffbf670d83f890212d6b272fa014a810 Mon Sep 17 00:00:00 2001 From: AlinaOktyabrskaya <44409263+AlinaOktyabrskaya@users.noreply.github.com> Date: Fri, 2 Nov 2018 23:50:29 +0300 Subject: [PATCH 2/2] Create Blockchain.java --- src/blockchain/Blockchain.java | 82 ++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/blockchain/Blockchain.java diff --git a/src/blockchain/Blockchain.java b/src/blockchain/Blockchain.java new file mode 100644 index 0000000..7a15583 --- /dev/null +++ b/src/blockchain/Blockchain.java @@ -0,0 +1,82 @@ +import java.security.MessageDigest; +import java.text.SimpleDateFormat; +import java.util.Calendar; +public class Blockchain { + + + Blockchain(){ + + + int id = 1; + int starthash = 0; + + int time = 3; + String previoushash = Integer.toString(starthash); + String currenthash = ""; + Boolean sec; + + + for(int i = 0; i < time; i ++) + { + System.out.println("Block:"); + System.out.println("id: " + id); + + currenthash = GenerateBlock(previoushash); + String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime().hashCode()); + System.out.println("Timestamp: " + timeStamp); + System.out.println("Hash of the previous block: " + previoushash); + System.out.println("Hash of the block: "+ currenthash); + + sec = validate(previoushash, currenthash); + + if (sec){ + previoushash = currenthash; + id++; + System.out.println(); + }else{ + System.out.println("Hash dont valid!!!"); + + break; + } + + + + + } + + + + + } + + private Boolean validate(String previousblock, String currentblock) { + String check = GenerateBlock(previousblock); + if(check.equals(currentblock)){ + return true; + }else { + return false; + } + + } + + private String GenerateBlock(String currentid) { + try { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + /* Applies sha256 to our input */ + byte[] hash = digest.digest(currentid.getBytes("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); + } + + } + + +}