Skip to content

Add HashData/TryHashData/HashDataAsync polyfills for MD5, SHA1, SHA256, SHA384, SHA512 - #126

Merged
Tyrrrz merged 15 commits into
primefrom
copilot/add-polyfills-for-hashdata-methods
Apr 21, 2026
Merged

Add HashData/TryHashData/HashDataAsync polyfills for MD5, SHA1, SHA256, SHA384, SHA512#126
Tyrrrz merged 15 commits into
primefrom
copilot/add-polyfills-for-hashdata-methods

Conversation

Copilot AI commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Polyfills the static HashData, TryHashData, and HashDataAsync methods introduced in .NET 5–7 for MD5, SHA1, SHA256, SHA384, and SHA512, enabling one-shot hashing without manually instantiating a HashAlgorithm.

Net50 — PolyShim/Net50/<Algorithm>.cs

Guard: (NETCOREAPP && !NET5_0_OR_GREATER) || NETFRAMEWORK || NETSTANDARD

  • HashData(byte[])byte[] (primary; creates the hasher and calls ComputeHash)
  • HashData(ReadOnlySpan<byte>)byte[] (delegates via ToArray())
  • HashData(ReadOnlySpan<byte>, Span<byte>)int
  • TryHashData(ReadOnlySpan<byte>, Span<byte>, out int)bool

Net50 — PolyShim/Net50/HashAlgorithm.cs

Guard: (NETCOREAPP && !NET5_0_OR_GREATER) || NETFRAMEWORK || NETSTANDARD

  • ComputeHashAsync(Stream, CancellationToken)Task<byte[]> (polyfilled via Task.Run)

Net70 — PolyShim/Net70/<Algorithm>.cs

Guard: (NETCOREAPP && !NET7_0_OR_GREATER) || NETFRAMEWORK || NETSTANDARD

  • HashData(Stream)byte[]
  • HashDataAsync(Stream, CancellationToken)ValueTask<byte[]> (delegates to HashAlgorithm.ComputeHashAsync)
  • HashDataAsync(Stream, Memory<byte>, CancellationToken)ValueTask<int>

Async methods are gated on #if FEATURE_TASK. All crypto-using code is gated on #if !NETSTANDARD || NETSTANDARD1_3_OR_GREATER since these types aren't available before netstandard1.3.

HashDataAsync(Stream, CT) creates a hash instance and delegates to ComputeHashAsync(Stream, CT), which is itself polyfilled via Task.Run(() => ComputeHash(stream), cancellationToken) for pre-.NET 5 targets.

// Before — required manual instantiation
byte[] hash;
using (var sha = SHA256.Create())
    hash = sha.ComputeHash(data);

// After — works on all target frameworks
var hash = SHA256.HashData(data);
var hash = await SHA256.HashDataAsync(stream, cancellationToken);

Copilot AI and others added 2 commits April 20, 2026 19:35
… SHA256, SHA384, SHA512

Agent-Logs-Url: https://github.com/Tyrrrz/PolyShim/sessions/c63e2bb4-0293-4830-ad77-809fd431f85c

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
…ream directly

Agent-Logs-Url: https://github.com/Tyrrrz/PolyShim/sessions/c63e2bb4-0293-4830-ad77-809fd431f85c

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Comment thread PolyShim.Tests/Net50/MD5Tests.cs Outdated
@Tyrrrz

Tyrrrz commented Apr 20, 2026

Copy link
Copy Markdown
Owner

@copilot delegate methods to each other as much as possible to reduce LOC and improve self-testing

… improve self-testing

Agent-Logs-Url: https://github.com/Tyrrrz/PolyShim/sessions/0d5e3b8b-9d21-4e50-880b-7b41375adbe7

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>

Copilot AI commented Apr 20, 2026

Copy link
Copy Markdown
Contributor Author

@copilot delegate methods to each other as much as possible to reduce LOC and improve self-testing

Done in e00a241. For all 5 Net50 algorithms the delegation chain is now:

  • HashData(byte[])HashData(ReadOnlySpan<byte>) (the core that creates the SHA instance)
  • HashData(ReadOnlySpan<byte>, Span<byte>)HashData(ReadOnlySpan<byte>) then copies
  • TryHashDataHashData(ReadOnlySpan<byte>, Span<byte>) via try/catch

Net70 was already well-chained (HashDataAsync(Stream, Memory)HashDataAsync(Stream)HashData(Stream)).

Comment thread PolyShim.Tests/Net50/MD5Tests.cs Outdated
…lyfill callees before callers

Agent-Logs-Url: https://github.com/Tyrrrz/PolyShim/sessions/f5d387d9-adf9-45b0-9ea5-d2ba0158a16c

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Copilot AI requested a review from Tyrrrz April 20, 2026 20:07
Comment thread PolyShim.Tests/Net50/MD5Tests.cs Outdated
Comment thread PolyShim.Tests/Net50/MD5Tests.cs Outdated
Comment thread PolyShim/Net50/MD5.cs Outdated
…below main TFM guard in polyfills

Agent-Logs-Url: https://github.com/Tyrrrz/PolyShim/sessions/15604d40-08a8-4a8c-beb9-539e378e5850

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Comment thread PolyShim/Net50/HashAlgorithm.cs Outdated
@Tyrrrz
Tyrrrz marked this pull request as ready for review April 21, 2026 17:05
Copilot AI review requested due to automatic review settings April 21, 2026 17:05
Copilot AI requested a review from Tyrrrz April 21, 2026 17:05
@codecov

codecov Bot commented Apr 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.34673% with 45 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.93%. Comparing base (deb0a71) to head (c855f2d).
⚠️ Report is 1 commits behind head on prime.

Files with missing lines Patch % Lines
PolyShim/Net50/MD5.cs 61.53% 4 Missing and 1 partial ⚠️
PolyShim/Net50/SHA1.cs 61.53% 4 Missing and 1 partial ⚠️
PolyShim/Net50/SHA256.cs 61.53% 4 Missing and 1 partial ⚠️
PolyShim/Net50/SHA384.cs 61.53% 4 Missing and 1 partial ⚠️
PolyShim/Net50/SHA512.cs 61.53% 4 Missing and 1 partial ⚠️
PolyShim/Net70/MD5.cs 69.23% 3 Missing and 1 partial ⚠️
PolyShim/Net70/SHA1.cs 69.23% 3 Missing and 1 partial ⚠️
PolyShim/Net70/SHA256.cs 69.23% 3 Missing and 1 partial ⚠️
PolyShim/Net70/SHA384.cs 69.23% 3 Missing and 1 partial ⚠️
PolyShim/Net70/SHA512.cs 69.23% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            prime     #126      +/-   ##
==========================================
+ Coverage   90.43%   90.93%   +0.49%     
==========================================
  Files         289      310      +21     
  Lines        5070     5866     +796     
  Branches      419      429      +10     
==========================================
+ Hits         4585     5334     +749     
- Misses        389      426      +37     
- Partials       96      106      +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds PolyShim polyfills for the one-shot hashing APIs introduced in newer .NET versions—HashData, TryHashData, and HashDataAsync—for MD5 and the SHA* family, so consumers can hash buffers/streams without manually creating HashAlgorithm instances.

Changes:

  • Added .NET 5-era HashData/TryHashData polyfills for MD5/SHA1/SHA256/SHA384/SHA512.
  • Added .NET 7-era stream-based HashData(Stream) and HashDataAsync(...) polyfills for the same algorithms.
  • Added/updated tests and refreshed Signatures.md to reflect the new surface area.

Reviewed changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
Signatures.md Updates the generated public API signature listing to include new hash APIs.
PolyShim/Net70/MD5.cs Adds .NET 7-style stream hashing APIs for MD5.
PolyShim/Net70/SHA1.cs Adds .NET 7-style stream hashing APIs for SHA1.
PolyShim/Net70/SHA256.cs Adds .NET 7-style stream hashing APIs for SHA256.
PolyShim/Net70/SHA384.cs Adds .NET 7-style stream hashing APIs for SHA384.
PolyShim/Net70/SHA512.cs Adds .NET 7-style stream hashing APIs for SHA512.
PolyShim/Net50/MD5.cs Adds .NET 5-style HashData/TryHashData polyfills for MD5.
PolyShim/Net50/SHA1.cs Adds .NET 5-style HashData/TryHashData polyfills for SHA1.
PolyShim/Net50/SHA256.cs Adds .NET 5-style HashData/TryHashData polyfills for SHA256.
PolyShim/Net50/SHA384.cs Adds .NET 5-style HashData/TryHashData polyfills for SHA384.
PolyShim/Net50/SHA512.cs Adds .NET 5-style HashData/TryHashData polyfills for SHA512.
PolyShim/Net50/HashAlgorithm.cs Adds a HashAlgorithm.ComputeHashAsync(Stream, CancellationToken) polyfill used by the stream hashing APIs.
PolyShim.Tests/Net70/MD5Tests.cs Tests the stream-based async hashing destination overload for MD5.
PolyShim.Tests/Net70/SHA1Tests.cs Tests the stream-based async hashing destination overload for SHA1.
PolyShim.Tests/Net70/SHA256Tests.cs Tests the stream-based async hashing destination overload for SHA256.
PolyShim.Tests/Net70/SHA384Tests.cs Tests the stream-based async hashing destination overload for SHA384.
PolyShim.Tests/Net70/SHA512Tests.cs Tests the stream-based async hashing destination overload for SHA512.
PolyShim.Tests/Net50/MD5Tests.cs Tests HashData and TryHashData behavior for MD5.
PolyShim.Tests/Net50/SHA1Tests.cs Tests HashData and TryHashData behavior for SHA1.
PolyShim.Tests/Net50/SHA256Tests.cs Tests HashData and TryHashData behavior for SHA256.
PolyShim.Tests/Net50/SHA384Tests.cs Tests HashData and TryHashData behavior for SHA384.
PolyShim.Tests/Net50/SHA512Tests.cs Tests HashData and TryHashData behavior for SHA512.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread PolyShim/Net70/MD5.cs
Comment thread PolyShim/Net70/SHA1.cs
Comment thread PolyShim/Net70/SHA256.cs
Comment thread PolyShim/Net70/SHA384.cs
Comment thread PolyShim/Net70/SHA512.cs
Comment thread Signatures.md Outdated
@Tyrrrz

Tyrrrz commented Apr 21, 2026

Copy link
Copy Markdown
Owner

@copilot please fix formatting errors

Agent-Logs-Url: https://github.com/Tyrrrz/PolyShim/sessions/933cba9e-88c6-4a65-b53a-d55e54e11a01

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>

Copilot AI commented Apr 21, 2026

Copy link
Copy Markdown
Contributor Author

@copilot please fix formatting errors

Done in c855f2d — ran dotnet format to fix all formatting errors flagged by CI (long lines split in SHA256/384/512, byte array literals expanded one-per-line in tests).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants