Guest User

Untitled

a guest
May 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using System.Linq;
  6.  
  7. namespace DoDChain
  8. {
  9. public class Block
  10. {
  11. int _index;
  12. DateTime _timeStamp;
  13. string _data;
  14. string _previousHash;
  15. string _hash;
  16.  
  17. public int Index { get => _index; }
  18. public string Hash { get => _hash; }
  19. public string PreviousHash { get => _previousHash; }
  20. public DateTime TimeStamp { get => _timeStamp; }
  21. public string Data { get => _data; }
  22.  
  23. public Block(
  24. int index,
  25. string data,
  26. string previousHash)
  27. {
  28. _index = index;
  29. _timeStamp = DateTime.Now;
  30. _data = data;
  31. _previousHash = previousHash;
  32. _hash = GetHash();
  33. }
  34.  
  35. public string GetHash()
  36. {
  37. return Hasher.Hash($"{_index}{_timeStamp}{_data}{_previousHash}");
  38. }
  39. }
  40.  
  41. public class Chain
  42. {
  43. List<Block> _blocks;
  44.  
  45. public List<Block> Blocks { get => _blocks; }
  46.  
  47. public Chain()
  48. {
  49. _blocks = new List<Block>();
  50. Block genesis = new Block(0, "Genesis Block", "0");
  51. _blocks.Add(genesis);
  52. }
  53.  
  54. public Block GetLastBlock()
  55. {
  56. return _blocks.Last();
  57. }
  58.  
  59. public void AddBlock(Block block)
  60. {
  61. _blocks.Add(block);
  62. }
  63.  
  64. public Result CheckValidity()
  65. {
  66. Result result = new Result();
  67.  
  68. for (int i = 0; i < _blocks.Count; i++)
  69. {
  70. var block = _blocks[i];
  71.  
  72. if (_blocks[i].GetHash() != block.Hash)
  73. {
  74. result.AddError($"Block at index {block.Index} has invalid hash.");
  75. }
  76.  
  77. if (i > 0)
  78. {
  79. var previousBlock = _blocks[i - 1];
  80.  
  81. if (block.Index != previousBlock.Index + 1)
  82. {
  83. result.AddError($"Wrong index at {block.Index}.");
  84. }
  85.  
  86. if (block.PreviousHash != previousBlock.Hash)
  87. {
  88. result.AddError($"Hashes don't match with previous at {block.Index}.");
  89. }
  90. }
  91. }
  92.  
  93. return result;
  94. }
  95. }
  96.  
  97. public class Hasher
  98. {
  99. public static string Hash(string input)
  100. {
  101. HashAlgorithm hasher = SHA256.Create();
  102. var hash = hasher.ComputeHash(Encoding.UTF8.GetBytes(input));
  103. var hashedString = BitConverter.ToString(hash).Replace("-", String.Empty);
  104. return hashedString;
  105. }
  106. }
  107.  
  108. public class Result
  109. {
  110. List<string> _errors;
  111.  
  112. public Result()
  113. {
  114. _errors = new List<string>();
  115. }
  116.  
  117. public List<string> Errors { get => _errors; }
  118.  
  119. public void AddError(string error)
  120. {
  121. _errors.Add(error);
  122. }
  123.  
  124. public bool Success
  125. {
  126. get
  127. {
  128. return _errors.Count() == 0;
  129. }
  130. }
  131. }
  132.  
  133. public class ChainServer
  134. {
  135. Chain chain = new Chain();
  136.  
  137. public void Add(string data)
  138. {
  139. var previousBlock = chain.GetLastBlock();
  140. chain.AddBlock(new Block(previousBlock.Index + 1, data, previousBlock.Hash));
  141. }
  142.  
  143. public Result CheckChainValidity()
  144. {
  145. return chain.CheckValidity();
  146. }
  147.  
  148. public List<Block> GetBlocks()
  149. {
  150. return chain.Blocks;
  151. }
  152. }
  153. }
Add Comment
Please, Sign In to add comment