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/4] 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/4] 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); + } + + } + + +} From c8461b43a17e20eb8633055b00f9bd4628922dca Mon Sep 17 00:00:00 2001 From: AlinaOktyabrskaya <44409263+AlinaOktyabrskaya@users.noreply.github.com> Date: Sat, 3 Nov 2018 23:18:31 +0300 Subject: [PATCH 3/4] Update Main.java --- src/blockchain/Main.java | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/blockchain/Main.java b/src/blockchain/Main.java index 127549e..26f99c3 100644 --- a/src/blockchain/Main.java +++ b/src/blockchain/Main.java @@ -1,7 +1,30 @@ package blockchain; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; + public class Main { + public static void main(String[] args) { - new Blockchain(); + new Blockchain(); + FileOutputStream fos = null; + ObjectOutputStream oos = null; + try { + fos = new FileOutputStream("temp.out"); + oos = new ObjectOutputStream(fos); + } catch (IOException e) { + System.out.println("Exit"); + } + Blockchain b = new Blockchain(); + + try { + oos.writeObject(b); + oos.flush(); + oos.close(); + } catch (IOException e) { + System.out.println("Smth wrong"); + } + + } } -} From f79fc7f08e714a4ba12b371d31bf0f2193f61452 Mon Sep 17 00:00:00 2001 From: AlinaOktyabrskaya <44409263+AlinaOktyabrskaya@users.noreply.github.com> Date: Sat, 3 Nov 2018 23:20:26 +0300 Subject: [PATCH 4/4] Update Blockchain.java --- src/blockchain/Blockchain.java | 83 +++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 22 deletions(-) diff --git a/src/blockchain/Blockchain.java b/src/blockchain/Blockchain.java index 7a15583..0082a17 100644 --- a/src/blockchain/Blockchain.java +++ b/src/blockchain/Blockchain.java @@ -1,62 +1,102 @@ +package blockchain; +import java.io.Serializable; import java.security.MessageDigest; -import java.text.SimpleDateFormat; -import java.util.Calendar; -public class Blockchain { +import java.time.LocalTime; +import java.util.Scanner; +public class Blockchain implements Serializable { - Blockchain(){ + private String nonce = ""; + int starthash = 0; + String previoushash = Integer.toString(starthash); + String currenthash = ""; + private int tiktok; - int id = 1; - int starthash = 0; + Blockchain() { + - int time = 3; - String previoushash = Integer.toString(starthash); - String currenthash = ""; + int id = 1; + int time; Boolean sec; + int zeros; + + Scanner scanner = new Scanner(System.in); + System.out.print("Enter how many zeros the hash must starts with: "); + zeros = scanner.nextInt(); + + for (int i = 0; i < zeros; i ++){ + nonce+="0"; + } + System.out.print("Enter how many block will created: "); + + time = scanner.nextInt(); + - for(int i = 0; i < time; i ++) - { + 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); + currenthash = proofOfWork(previoushash); + getTimeStamp(); + System.out.println("Hash of the previous block: " + previoushash); - System.out.println("Hash of the block: "+ currenthash); + System.out.println("Hash of the block: " + currenthash); + System.out.println("Block was generating for " + tiktok + " seconds"); sec = validate(previoushash, currenthash); - if (sec){ + if (sec) { previoushash = currenthash; id++; System.out.println(); - }else{ - System.out.println("Hash dont valid!!!"); + } else { + System.out.println("Hash not valid!!!"); break; } + } + } + private void getTimeStamp() { + System.out.println("Timestamp: " + System.currentTimeMillis()); + } + private String proofOfWork(String block) { - } + String nonceKey = nonce; + long nonce = 0; + boolean nonceFound = false; + String nonceHash = ""; + + + LocalTime time_before = LocalTime.now(); + while (!nonceFound) { + nonceHash = GenerateBlock(block + nonce); + nonceFound = nonceHash.substring(0, nonceKey.length()).equals(nonceKey); + nonce++; + + } + + LocalTime time_after = LocalTime.now(); + //считаем разницу во времени + this.tiktok = (time_after.getMinute()-time_before.getMinute())*60 + (time_after.getSecond()-time_before.getSecond()); + return nonceHash; } private Boolean validate(String previousblock, String currentblock) { - String check = GenerateBlock(previousblock); + String check = proofOfWork(previousblock); if(check.equals(currentblock)){ return true; }else { return false; } - } private String GenerateBlock(String currentid) { @@ -78,5 +118,4 @@ private String GenerateBlock(String currentid) { } - }