Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. using System;
  2. using System.Security.Cryptography;
  3.  
  4. namespace Models
  5. {
  6. public class Block
  7. {
  8. public int Index { get; set; }
  9. public Int64 TimeStamp { get; set; }
  10. public byte[] PrevHash { get; set; }
  11. public byte[] Hash { get; set; }
  12. public byte[] Transaction { get; set; }
  13.  
  14. public Block( byte[] prevHash, byte[] transaction)
  15. {
  16. Index = 0;
  17. PrevHash = prevHash;
  18. TimeStamp = DateTime.Now.Ticks;
  19. Transaction = transaction;
  20. Hash = GenerateHash();
  21. }
  22.  
  23. public byte[] GenerateHash()
  24. {
  25. SHA256 sha256 = SHA256.Create();
  26. byte[] tStamp = BitConverter.GetBytes(TimeStamp);
  27. byte[] sumBytes = new byte[tStamp.Length + PrevHash.Length + Transaction.Length];
  28.  
  29. System.Buffer.BlockCopy(tStamp, 0, sumBytes, 0, tStamp.Length);
  30. System.Buffer.BlockCopy(PrevHash, 0, sumBytes, tStamp.Length, PrevHash.Length);
  31. System.Buffer.BlockCopy(Transaction, 0, sumBytes, tStamp.Length + PrevHash.Length, Transaction.Length);
  32.  
  33. byte[] hashBytes = sha256.ComputeHash(sumBytes);
  34. return hashBytes;
  35. }
  36.  
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement