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];
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;
}
}
}
source
share