title: How to compute Hash (MD5 or SHA) of a large file with C# in Windows Store App
body: <p><strong>PROBLEM:</strong></p>
  
  <p>"If you try to calculate md5 or sha in a Windows 8 Metro App using the metod HashData(IBuffer) with a buffer that contains a large file, you receive the OutOfMemoryException because the buffer is very big (contains a copy in byte of the original file)."</p>
  
  <p><strong>SOLUTION:</strong></p>
  
  <pre><code>//NB: "file" is a "StorageFile" previously openedHashAlgorithmProvider md5 = Windows.Security.Cryptography.Core.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
  //in this example I use HashAlgorithmNames.Md5, you can replace it with HashAlgorithmName.Sha1, etc...
  
  HashAlgorithmProvider alg = Windows.Security.Cryptography.Core.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
  var stream = await file.OpenStreamForReadAsync();
  var inputStream = stream.AsInputStream();
  uint capacity = 100000000;
  Windows.Storage.Streams.Buffer buffer = new Windows.Storage.Streams.Buffer(capacity);
  var hash = alg.CreateHash();
  
  while (true)
  {
      await inputStream.ReadAsync(buffer, capacity, InputStreamOptions.None);
      if (buffer.Length &gt; 0)
          hash.Append(buffer);
      else
          break;
  }
  
  string hashText = CryptographicBuffer.EncodeToHexString(hash.GetValueAndReset()).ToUpper();
  
  inputStream.Dispose();
  stream.Dispose();
  </code></pre>
  
  <p>I hope that this is helpful :)</p>