❔ Increase Hash computing speed.
So I am trying to make a simple app for my use case that computes SHA256 of two files. right now I have this following code which works but is extremely slow:
I have only set it to get the hash for one file. my issue is the speed of the hash computing is extremely slow. is there any way by which I can increase the speed ?
18 Replies
i dont see much u could improve:
fs.Position = 0;
can be removed as u are always starting at pos 0 when reading the file
here u are creating a 4kb array, just to replace it with the array from the hasher
so byte[] hash = mySHA256.ComputeHash(fs);
would be enough
besides that i dont think you can improve anything else that easily and the most impact comes probably from the file size and generally reading from the diskYeah getting a large file's hash is the part tht is quite slow. I want the speed to atleast be on par with 7zip's CRC-SHA>SHA256 option and the current code is slower than tht.
the only thing i can think of, would be to do it async and in chunks.
basically read a chunk of the file, fire off reading the next chunk async and while that task is running compute the hash of the already read chunk
not sure if u can do that with that SHA256 implementation or if u have to roll ur own
basically start computing the hash while continuing to read the file
you mean like doing it in a while loop with a custom bytes to read value per chunk ?
I was thinking of using a buffered stream and set a large buffer length instead of being relegated to just 4kb.
that wont work, because it still will either read or compute the hash when u look at the implementation:
but u want to do both at the same time
basically in the while loop u would not read directly from the stream, but from some kind of list/queue that contains the chunks, while another thread/an async running task is reading from the stream and filling the list/queue
that approach would have a little overhead because of synchronizing between the 2 threads and would only save some time while the
HashCore
method is computing that chunkgetting lot of errors with this code. does this work on a framework project ?
thats the dotnet 7 version of
SHA256.ComputeHash
, but u would not want to use that code anyway, as it is synchronously reading a chunk from the stream and then computing the hash
not sure whats all available on net 4.6 framework
well the main issue is still speeding up the hashing + file reading
u would want something like this for doing both in parallel:
iirc ArrayPool
isnt available in framework, so i simply create a new byte array for each chunk
DoHashing()
would would be like the HashCore()
method, just that i passed the result array as parameter as well
i also omitted the cancellation token stuff to show the important stuff
note also that this totally disregards memory consumption, if u want to limited it to eg, have only 5 chunks in memory at the same time u could use a SemaphoreSlim
for thatis DoHashing supposed to be a method ?
one u would have to implement ;p
its just a place holder to show where the hashing belongs
oh so I would have to put this method in the DoHashing method then ?
no
u would have to use
HashCore()
, but because that not public u would have to implement the hashing algorithm urself as wellsorry I don't know how to write my own hashing algorithm. is there a documentation page that I can refer to and learn more about this ?
this is just untested example code to show how to set it up to read the file contents and do hashing in parallel, the details u would have to work out urself
i dunno how to implement it either, just search online, i guess there are tons of example implementations
so when you mean implement a hashing algorithm, is it like make something new or use the existing SHA256 algorithm ?
maybe there is also just a package out there in the wild that has a faster implementation, then S.S.C.SHA256
yeah, just implement sha256 urself, so that u can hash chunk by chunk
I would love to do that but I am time bound now. guess I will stick with the slow speed itself.
I will try looking for a package that has a chunk by chunk system implemented.
if you are using .NET Framework then you are already giving up considerable performance
have you tried using
SHA256.Create("System.Security.Cryptography.SHA256Cng")
instead of SHA256.Create()
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.