Guest User

Untitled

a guest
Jan 22nd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. public class CalculatingSha256
  2. {
  3. private static object lockObj = new object();
  4. private static SortedList sha256DataList;
  5.  
  6. public CalculatingSha256()
  7. {
  8. sha256DataList = new SortedList();
  9. sha256DataList = SortedList.Synchronized(sha256DataList);
  10. }
  11.  
  12. public void CalculateSha256(PartBlock block)
  13. {
  14. int index = sha256DataList.IndexOfKey(block.IdFullBlock);
  15. SHA256Data sha256Data = null;
  16.  
  17. if (index >= 0)
  18. {
  19. sha256Data = (SHA256Data)sha256DataList.GetByIndex(index);
  20. }
  21.  
  22. if (sha256Data == null)
  23. {
  24. sha256Data = new SHA256Data();
  25. if (block.SizeFullBlock != 0)
  26. {
  27. sha256Data.BlockSize = block.SizeFullBlock;
  28. }
  29. sha256Data.Sha256 = new SHA256Managed();
  30. lock (lockObj)
  31. {
  32. sha256DataList.Add(block.IdFullBlock, sha256Data);
  33. }
  34. }
  35.  
  36. Console.WriteLine(block.Id+" "+block.SizeFullBlock+" "+block.IdFullBlock);
  37. sha256Data.Sha256.TransformBlock(block.Data, 0, block.Size, block.Data, 0);
  38. sha256Data.CurrentCalculated += block.Size;
  39. bool bBlockCalculateIsOver = sha256Data.CurrentCalculated == sha256Data.BlockSize;
  40.  
  41. if ((sha256Data.BlockSize != 0) && (bBlockCalculateIsOver))
  42. {
  43. byte[] input = new byte[0];
  44. sha256Data.Sha256.TransformFinalBlock(input, 0, 0);
  45. string hashString = ToString(sha256Data.Sha256.Hash);
  46. Console.WriteLine("Блок номер {0}:{1}", block.IdFullBlock+1, hashString.ToUpper());
  47. sha256DataList.Remove(block.IdFullBlock);
  48. }
  49. }
  50. }
  51.  
  52. public abstract class Block
  53. {
  54. public int Id { get; set; }
  55. }
  56.  
  57. public class PartBlock : Block
  58. {
  59. public int Size { get; set; }
  60. public byte[] Data { get; set; }
  61. //номер целого блока которому принадлежит
  62. public int IdFullBlock { get; set; }
  63. public int SizeFullBlock { get; set; }
  64. }
  65.  
  66. public class SHA256Data
  67. {
  68. public int BlockSize { get; set; }
  69. public int CurrentCalculated { get; set; }
  70. public SHA256Managed Sha256 { get; set; }
  71. }
  72.  
  73. byte[] hash;
  74. using (var fileStream = File.Open(path))
  75. hash = SHA256Managed.Create().ComputeHash(fileStream);
Add Comment
Please, Sign In to add comment