Repository navigation
Make HashDataAsync, HmacDataAsync and VerifyHmacAsync asynchronous and cancellable - #655
Merged
Merged
Conversation
…usly and honour cancellation
This was referenced Oct 12, 2026
This was referenced Oct 13, 2026
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
The
HashDataAsyncpolyfills onMD5,SHA1,SHA256,SHA384andSHA512, andHashDataAsync/HmacDataAsync/VerifyHmacAsynconCryptographicOperations(both the recreated type and the extension members), were not asynchronous:ComputeHash(Stream)and wrapped the result in a completedValueTask, blocking the caller for the whole read. A stream that only supports asynchronous reads could not be used.SHA256.HashDataAsync(Stream, CancellationToken)did not check it at all.OperationCanceledExceptionsynchronously. The BCL returns a cancelled task.Fix
A shared internal helper (
Polyfill_HashAlgorithm.cs) reads the stream withReadAsync, passing the token, and feedsTransformBlock/TransformFinalBlock. All of the async members now go through it.As in the BCL, arguments are validated synchronously (null
source, nullkey/hash, a destination that is too short, ahashof the wrong length), and a pre-cancelled token gives a cancelled task without touching the stream. The behaviour was checked against net11 first.Test
AsyncOnlyStreamis a test stream that throws on a synchronousRead, returns a byte perReadAsync, and observes the token. New tests in theMD5,SHA1,SHA256,SHA384,SHA512andCryptographicOperationstest classes cover:All pass on net11.0 (BCL). On net10.0, where only
VerifyHmacAsyncis polyfilled, exactly theVerifyHmacAsynctests failed before the fix. They pass on net462, net48, net8.0, net9.0 and net10.0 with the fix.