Ultra Geile Studenten Benutzer Oberfläche (UGSBO)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package ugsbo.com.buchhaltung;
  2. import java.security.MessageDigest;
  3. public class Block {
  4. int data;
  5. int kontostand;
  6. Block vorher;
  7. String ownHash;
  8. String previousHash;
  9. //erste Block
  10. public Block(int data) {
  11. this.data = data;
  12. ownHash = createNewHash(Integer.toString(data));
  13. kontostand = this.data;
  14. this.vorher = null;
  15. this.previousHash = null;
  16. }
  17. //Alle anderen Blöcke
  18. public Block(int data, Block vorher, String previousHash, int konto) {
  19. this.data = data;
  20. ownHash = createNewHash(Integer.toString(data)+previousHash);
  21. kontostand = konto + this.data;
  22. this.vorher = vorher;
  23. this.previousHash = previousHash;
  24. }
  25. private String createNewHash(String input) {
  26. try {
  27. MessageDigest digest = MessageDigest.getInstance("SHA-256");
  28. //Applies sha256 to our input,
  29. byte[] hash = digest.digest(input.getBytes("UTF-8"));
  30. StringBuffer hexString = new StringBuffer(); // This will contain hash as hexidecimal
  31. for (int i = 0; i < hash.length; i++) {
  32. String hex = Integer.toHexString(0xff & hash[i]);
  33. if(hex.length() == 1) hexString.append('0');
  34. hexString.append(hex);
  35. }
  36. return hexString.toString();
  37. }
  38. catch(Exception e) {
  39. throw new RuntimeException(e);
  40. }
  41. }
  42. public int getKontostand() {
  43. return kontostand;
  44. }
  45. public Block getVorher() {
  46. return vorher;
  47. }
  48. public String getHash() {
  49. return ownHash;
  50. }
  51. }