1

I am using C# in Windows Mobile 5. The program downloads large files from the internet along with their hash values. Then it computes a hash value of the downloaded data and compares it to the expected hash value. The idea is to verify that the entire file was downloaded uncorrupted.

The problem is that the file is large enough to where if I put the entire contents of the file into memory in a byte array, then the device will run out of memory. However I want to do this so that I can compute the hash of the bytes. Is it possible to compute the hash without having all the bytes in memory at once? Preferably I would like to compute SHA1 hashes using the SHA1Managed class, but I am willing to change that if necessary. I did notice that there is an overload of the SHA1Managed.ComputeHash() method that accepts a Stream, but I don't know if it uses any less memory than just pulling all the bytes into memory and the memory profilers I know of for .NET CF are completely useless.

still_dreaming_1
  • 8,661
  • 6
  • 39
  • 56

2 Answers2

3

SHA1Managed.ComputeHash(Stream) should be more memory efficient assuming that you're discarding the contents of the stream after you compute your hash value. How much memory you will use will be based in part on the Stream implementation.

John Percival Hackworth
  • 11,395
  • 2
  • 29
  • 38
  • What would be a good Stream implementation for this if the data is already written to a file? Is it fine to just use File.OpenRead(fileName) which returns a FileStream? – still_dreaming_1 Apr 23 '11 at 00:01
  • I tried it with a large enough file to where it would have run out of memory if the Stream implementation been pulling the entire file into memory. It worked, Thanks! – still_dreaming_1 Apr 23 '11 at 00:42
1

Here is how it's done on the desktop:

Compute a hash from a stream of unknown length in C#

It should be pretty easy to test whether the stream implementation pulls in the entire file or not by using an input source larger than the amount of memory available.

Community
  • 1
  • 1
user423430
  • 3,654
  • 3
  • 26
  • 22