How to hash a single file in several ways at the same time?

I am trying to create a simple application that will be used to calculate the CRC32 / md5 / sha1 / sha256 / sha384 / sha512 file, and I came across a small hurdle. This is done in C #.

I would like to do this as efficiently as possible, so my initial thought was to read the file in memystream first before processing, but I soon found out that very large files make me run out of memory quickly. Therefore, it seems to me that I should use a filter. The problem, as I see it, is that you can only run one hash function at a time, and with the stream of chips it will take some time to complete each hash.

How can I read a small part of a file in memory, process it with all 6 algorithms, and then move to another fragment ... Or does hashing not work this way?

This was my initial attempt to read a file in memory. It failed when I tried to read the CD image in memory before running the hashing algorithms in memystream:

    private void ReadToEndOfFile(string filename)
    {
        if (File.Exists(filename))
        {
            FileInfo fi = new FileInfo(filename);
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[16 * 1024];

            //double step = Math.Floor((double)fi.Length / (double)100);

            this.toolStripStatusLabel1.Text = "Reading File...";
            this.toolStripProgressBar1.Maximum = (int)(fs.Length / buffer.Length);
            this.toolStripProgressBar1.Value = 0;

            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = fs.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                    this.toolStripProgressBar1.Value += 1;
                }

                _ms = ms;
            }
        }
    }
+5
source share
3 answers

You are most on the way, you just do not need to immediately read all this in memory.

.Net HashAlgorithm. : TransformBlock TransformFinalBlock. , , TransformBlock , , . TransformFinalBlock , , , .

, , ( - )

+3

, . #/.NET . .

+4

, TPL. BroadcastBlock<T>. BroadcastBlock<T> 6 ActionBlock<T>. ActionBlock<T> 6 -.

var broadcast = new BroadcastBlock<byte[]>(x => x);

var strategy1 = new ActionBlock<byte[]>(input => DoHash(input, SHA1.Create()));
var strategy2 = new ActionBlock<byte[]>(input => DoHash(input, MD5.Create()));
// Create the other 4 strategies.

broadcast.LinkTo(strategy1);
broadcast.LinkTo(strategy2);
// Link the other 4.

using (var fs = File.Open(@"yourfile.txt", FileMode.Open, FileAccess.Read))
using (var br = new BinaryReader(fs))
{
  while (br.PeekChar() != -1)
  {
    broadcast.Post(br.ReadBytes(1024 * 16));
  }
}

BroadcastBlock<T> ActionBlock<T>.

, , DoHash .

private void DoHash(byte[] input, HashAlgorithm algorithm)
{
  // You will need to implement this.
}
0

All Articles