Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CalculatingSha256
- {
- private static object lockObj = new object();
- private static SortedList sha256DataList;
- public CalculatingSha256()
- {
- sha256DataList = new SortedList();
- sha256DataList = SortedList.Synchronized(sha256DataList);
- }
- public void CalculateSha256(PartBlock block)
- {
- int index = sha256DataList.IndexOfKey(block.IdFullBlock);
- SHA256Data sha256Data = null;
- if (index >= 0)
- {
- sha256Data = (SHA256Data)sha256DataList.GetByIndex(index);
- }
- if (sha256Data == null)
- {
- sha256Data = new SHA256Data();
- if (block.SizeFullBlock != 0)
- {
- sha256Data.BlockSize = block.SizeFullBlock;
- }
- sha256Data.Sha256 = new SHA256Managed();
- lock (lockObj)
- {
- sha256DataList.Add(block.IdFullBlock, sha256Data);
- }
- }
- Console.WriteLine(block.Id+" "+block.SizeFullBlock+" "+block.IdFullBlock);
- sha256Data.Sha256.TransformBlock(block.Data, 0, block.Size, block.Data, 0);
- sha256Data.CurrentCalculated += block.Size;
- bool bBlockCalculateIsOver = sha256Data.CurrentCalculated == sha256Data.BlockSize;
- if ((sha256Data.BlockSize != 0) && (bBlockCalculateIsOver))
- {
- byte[] input = new byte[0];
- sha256Data.Sha256.TransformFinalBlock(input, 0, 0);
- string hashString = ToString(sha256Data.Sha256.Hash);
- Console.WriteLine("Блок номер {0}:{1}", block.IdFullBlock+1, hashString.ToUpper());
- sha256DataList.Remove(block.IdFullBlock);
- }
- }
- }
- public abstract class Block
- {
- public int Id { get; set; }
- }
- public class PartBlock : Block
- {
- public int Size { get; set; }
- public byte[] Data { get; set; }
- //номер целого блока которому принадлежит
- public int IdFullBlock { get; set; }
- public int SizeFullBlock { get; set; }
- }
- public class SHA256Data
- {
- public int BlockSize { get; set; }
- public int CurrentCalculated { get; set; }
- public SHA256Managed Sha256 { get; set; }
- }
- byte[] hash;
- using (var fileStream = File.Open(path))
- hash = SHA256Managed.Create().ComputeHash(fileStream);
- class PartialFileStream : Stream
- {
- public PartialFileStream(FileStream fs, long start, long length)
- {
- if (!fs.CanSeek || !fs.CanRead)
- throw new ArgumentException("Seekable and readable stream required",
- nameof(fs));
- this.fs = fs;
- fs.Position = start;
- this.PartStart = start;
- this.PartLength = length;
- }
- FileStream fs;
- long PartStart, PartLength;
- public override bool CanRead => true;
- public override bool CanSeek => true;
- public override bool CanWrite => false;
- public override long Length => PartLength;
- public override long Position
- {
- get { return fs.Position - PartStart; }
- set { fs.Position = value + PartStart; }
- }
- public override void Flush() => fs.Flush();
- public override int Read(byte[] buffer, int offset, int count)
- {
- var maxAllowedLength = PartLength - Position;
- return fs.Read(buffer, offset, (int)Math.Min(count, maxAllowedLength));
- }
- public override long Seek(long offset, SeekOrigin origin)
- {
- switch (origin)
- {
- case SeekOrigin.Begin:
- return fs.Seek(offset + PartStart, SeekOrigin.Begin) - PartStart;
- case SeekOrigin.Current:
- return fs.Seek(offset, SeekOrigin.Current) - PartStart;
- case SeekOrigin.End:
- return fs.Seek(offset + PartStart + PartLength, SeekOrigin.Begin) - PartStart;
- }
- throw new ArgumentException(nameof(origin));
- }
- public override void SetLength(long value) { throw new NotSupportedException(); }
- public override void Write(byte[] buffer, int offset, int count)
- { throw new NotSupportedException(); }
- }
- class Program
- {
- static void Main(string[] args)
- {
- new Program().Run().Wait();
- }
- async Task Run()
- {
- var path = тут-путь-к-файлу;
- long size = new FileInfo(path).Length;
- long chunkSize = тут-размер-одного-куска;
- long nchunks = (size + chunkSize - 1) / chunkSize;
- var encodeTasks = new List<Task<byte[]>>();
- for (int i = 0; i < nchunks; i++)
- {
- var length = chunkSize;
- if (i == nchunks - 1) // last chunk
- length = size - i * chunkSize;
- encodeTasks.Add(Task.Run(() => ComputeHash(path, i * chunkSize, length)));
- }
- var results = await Task.WhenAll(encodeTasks);
- // тут у вас есть массив хешей, делайте с ним что-то
- }
- byte[] ComputeHash(string path, long startPos, long length)
- {
- using (var fileStream = File.Open(path))
- using (var partialStream = new PartialFileStream(fileStream, startPos, length))
- return SHA256Managed.Create().ComputeHash(partialStream);
- }
- }
- byte[] ComputeHash(string path, long startPos, long length)
- {
- using (var fs = File.OpenRead(path))
- {
- fs.Position = startPos;
- using (var sha = SHA256Managed.Create())
- {
- var buffer = new byte[4096];
- long bytesToGo = length;
- while (bytesToGo > 0)
- {
- var bytesRead = fs.Read(buffer, 0, (int)Math.Min(buffer.Length, bytesToGo));
- if (bytesRead == 0)
- throw new EndOfStreamException();
- sha.TransformBlock(buffer, 0, bytesRead, null, 0);
- bytesToGo -= bytesRead;
- }
- sha.TransformFinalBlock(buffer, 0, 0);
- return sha.Hash;
- }
- }
- }
Add Comment
Please, Sign In to add comment