Guest User

Untitled

a guest
Jan 23rd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 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);
  76.  
  77. class PartialFileStream : Stream
  78. {
  79. public PartialFileStream(FileStream fs, long start, long length)
  80. {
  81. if (!fs.CanSeek || !fs.CanRead)
  82. throw new ArgumentException("Seekable and readable stream required",
  83. nameof(fs));
  84. this.fs = fs;
  85. fs.Position = start;
  86. this.PartStart = start;
  87. this.PartLength = length;
  88. }
  89.  
  90. FileStream fs;
  91. long PartStart, PartLength;
  92.  
  93. public override bool CanRead => true;
  94. public override bool CanSeek => true;
  95. public override bool CanWrite => false;
  96. public override long Length => PartLength;
  97.  
  98. public override long Position
  99. {
  100. get { return fs.Position - PartStart; }
  101. set { fs.Position = value + PartStart; }
  102. }
  103.  
  104. public override void Flush() => fs.Flush();
  105.  
  106. public override int Read(byte[] buffer, int offset, int count)
  107. {
  108. var maxAllowedLength = PartLength - Position;
  109. return fs.Read(buffer, offset, (int)Math.Min(count, maxAllowedLength));
  110. }
  111.  
  112. public override long Seek(long offset, SeekOrigin origin)
  113. {
  114. switch (origin)
  115. {
  116. case SeekOrigin.Begin:
  117. return fs.Seek(offset + PartStart, SeekOrigin.Begin) - PartStart;
  118. case SeekOrigin.Current:
  119. return fs.Seek(offset, SeekOrigin.Current) - PartStart;
  120. case SeekOrigin.End:
  121. return fs.Seek(offset + PartStart + PartLength, SeekOrigin.Begin) - PartStart;
  122. }
  123. throw new ArgumentException(nameof(origin));
  124. }
  125.  
  126. public override void SetLength(long value) { throw new NotSupportedException(); }
  127. public override void Write(byte[] buffer, int offset, int count)
  128. { throw new NotSupportedException(); }
  129. }
  130.  
  131. class Program
  132. {
  133. static void Main(string[] args)
  134. {
  135. new Program().Run().Wait();
  136. }
  137.  
  138. async Task Run()
  139. {
  140. var path = тут-путь-к-файлу;
  141. long size = new FileInfo(path).Length;
  142. long chunkSize = тут-размер-одного-куска;
  143. long nchunks = (size + chunkSize - 1) / chunkSize;
  144. var encodeTasks = new List<Task<byte[]>>();
  145. for (int i = 0; i < nchunks; i++)
  146. {
  147. var length = chunkSize;
  148. if (i == nchunks - 1) // last chunk
  149. length = size - i * chunkSize;
  150. encodeTasks.Add(Task.Run(() => ComputeHash(path, i * chunkSize, length)));
  151. }
  152. var results = await Task.WhenAll(encodeTasks);
  153. // тут у вас есть массив хешей, делайте с ним что-то
  154. }
  155.  
  156. byte[] ComputeHash(string path, long startPos, long length)
  157. {
  158. using (var fileStream = File.Open(path))
  159. using (var partialStream = new PartialFileStream(fileStream, startPos, length))
  160. return SHA256Managed.Create().ComputeHash(partialStream);
  161. }
  162. }
  163.  
  164. byte[] ComputeHash(string path, long startPos, long length)
  165. {
  166. using (var fs = File.OpenRead(path))
  167. {
  168. fs.Position = startPos;
  169. using (var sha = SHA256Managed.Create())
  170. {
  171. var buffer = new byte[4096];
  172. long bytesToGo = length;
  173. while (bytesToGo > 0)
  174. {
  175. var bytesRead = fs.Read(buffer, 0, (int)Math.Min(buffer.Length, bytesToGo));
  176. if (bytesRead == 0)
  177. throw new EndOfStreamException();
  178. sha.TransformBlock(buffer, 0, bytesRead, null, 0);
  179. bytesToGo -= bytesRead;
  180. }
  181. sha.TransformFinalBlock(buffer, 0, 0);
  182. return sha.Hash;
  183. }
  184. }
  185. }
Add Comment
Please, Sign In to add comment