Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ramonsmits
Created April 16, 2019 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramonsmits/09eb916e97b714b44dadf12d83ce6c06 to your computer and use it in GitHub Desktop.
Save ramonsmits/09eb916e97b714b44dadf12d83ce6c06 to your computer and use it in GitHub Desktop.
ComputeHashAsync extension as there is no async version in .net framework or .net core
using System.IO;
using System.Threading.Tasks;
namespace System.Security.Cryptography
{
public static class HashAlgorithmExt
{
public static async Task<byte[]> ComputeHashAsync(this HashAlgorithm algorithm, Stream stream, int blockSize = 0x14000)
{
var buffer = new byte[blockSize];
int read;
while ((read = await stream.ReadAsync(buffer, 0, blockSize).ConfigureAwait(false)) == blockSize)
algorithm.TransformBlock(buffer, 0, read, buffer, 0);
algorithm.TransformFinalBlock(buffer, 0, read);
return algorithm.Hash;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment