Created
April 16, 2019 14:24
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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