Browse Source

Blockchain Logik fertig

pullrequest
Christian Baltzer 5 years ago
parent
commit
5fa8bbf2b7
  1. 41
      src/main/java/ugsbo/com/buchhaltung/Block.java
  2. 9
      src/main/java/ugsbo/com/buchhaltung/Blockchain.java

41
src/main/java/ugsbo/com/buchhaltung/Block.java

@ -12,19 +12,11 @@ public class Block {
String ownHash; String ownHash;
String previousHash; String previousHash;
public Block(int data, Block vorher, String previousHash, int konto) {
this.data = data;
ownHash = createNewHash();
kontostand = konto + this.data;
this.vorher = vorher;
this.previousHash = previousHash;
}
//erste Block
public Block(int data) { public Block(int data) {
this.data = data; this.data = data;
ownHash = createNewHash();
ownHash = createNewHash(Integer.toString(data));
kontostand = this.data; kontostand = this.data;
@ -32,8 +24,33 @@ public class Block {
this.previousHash = null; this.previousHash = null;
} }
private String createNewHash() {
return null;
//Alle anderen Blöcke
public Block(int data, Block vorher, String previousHash, int konto) {
this.data = data;
ownHash = createNewHash(Integer.toString(data)+previousHash);
kontostand = konto + this.data;
this.vorher = vorher;
this.previousHash = previousHash;
}
private String createNewHash(String input) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
//Applies sha256 to our input,
byte[] hash = digest.digest(input.getBytes("UTF-8"));
StringBuffer hexString = new StringBuffer(); // This will contain hash as hexidecimal
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(0xff & hash[i]);
if(hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
}
catch(Exception e) {
throw new RuntimeException(e);
}
} }
public int getKontostand() { public int getKontostand() {

9
src/main/java/ugsbo/com/buchhaltung/Blockchain.java

@ -1,5 +1,7 @@
package ugsbo.com.buchhaltung; package ugsbo.com.buchhaltung;
import com.google.gson.*;
public class Blockchain { public class Blockchain {
Block Workingobjekt; Block Workingobjekt;
@ -11,7 +13,6 @@ public class Blockchain {
public void add(int eingabe) { public void add(int eingabe) {
// TODO Auto-generated method stub
Block newWorkingobjekt = new Block(eingabe, Workingobjekt, Workingobjekt.getHash(), Workingobjekt.getKontostand()); Block newWorkingobjekt = new Block(eingabe, Workingobjekt, Workingobjekt.getHash(), Workingobjekt.getKontostand());
Workingobjekt = newWorkingobjekt; Workingobjekt = newWorkingobjekt;
@ -19,14 +20,12 @@ public class Blockchain {
public int kontostand() { public int kontostand() {
// TODO Auto-generated method stub
return Workingobjekt.getKontostand(); return Workingobjekt.getKontostand();
} }
public String toString() { public String toString() {
return null;
String JSON = new GsonBuilder().setPrettyPrinting().create().toJson(Workingobjekt);
return JSON;
} }

Loading…
Cancel
Save