-
Notifications
You must be signed in to change notification settings - Fork 5
Add HashData/TryHashData/HashDataAsync polyfills for MD5, SHA1, SHA256, SHA384, SHA512 #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
96999d5
feat: add HashData/TryHashData/HashDataAsync polyfills for MD5, SHA1,…
Copilot 4d27b63
perf: avoid ToArray() allocation in HashDataAsync by reusing MemorySt…
Copilot bde7cee
test: replace circular hash assertions with precomputed expected values
Copilot e00a241
refactor: delegate Net50 hash methods to each other to reduce LOC and…
Copilot b07dd97
refactor: inline hash bytes, keep only outer-method tests, reorder po…
Copilot 972daa7
rename HashData_Array_Test to HashData_Test in all Net50 test files
Copilot ded739e
remove inline hash-label comments from tests; move netstandard guard …
Copilot 67e75bb
rename HashDataAsync_Stream_WithDestination_Test to HashDataAsync_Tes…
Copilot a34d47c
make HashData(byte[]) the primary overload in all 5 Net50 algorithms;…
Copilot df6bf2f
restore netstandard guard comments in all 10 Net50/Net70 polyfill files
Copilot 3283428
add HashAlgorithm.ComputeHashAsync polyfill; use it in Net70 HashData…
Copilot 8ff61fb
move FEATURE_TASK guard to method level in HashAlgorithm.cs
Copilot dfb677f
use async/await in ComputeHashAsync polyfill
Copilot 1ee977e
add ConfigureAwait(false) to ComputeHashAsync polyfill
Copilot c855f2d
Fix formatting: run dotnet format
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| using System; | ||
| using System.Security.Cryptography; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PolyShim.Tests.Net50; | ||
|
|
||
| public class MD5Tests | ||
| { | ||
| // MD5([1, 2, 3, 4, 5]) | ||
| private static readonly byte[] ExpectedHash = | ||
| [ | ||
| 0x7c, | ||
| 0xfd, | ||
| 0xd0, | ||
| 0x78, | ||
| 0x89, | ||
| 0xb3, | ||
| 0x29, | ||
| 0x5d, | ||
| 0x6a, | ||
| 0x55, | ||
| 0x09, | ||
| 0x14, | ||
| 0xab, | ||
| 0x35, | ||
| 0xe0, | ||
| 0x68, | ||
| ]; | ||
|
|
||
| [Fact] | ||
| public void HashData_Array_Test() | ||
|
Tyrrrz marked this conversation as resolved.
Outdated
|
||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5 }; | ||
|
|
||
| // Act | ||
| var hash = MD5.HashData(data); | ||
|
|
||
| // Assert | ||
| hash.Should().Equal(ExpectedHash); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void HashData_Span_Test() | ||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5 }; | ||
|
|
||
| // Act | ||
| var hash = MD5.HashData(data.AsSpan()); | ||
|
|
||
| // Assert | ||
| hash.Should().Equal(ExpectedHash); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void HashData_Span_WithDestination_Test() | ||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5 }; | ||
| var destination = new byte[16]; | ||
|
|
||
| // Act | ||
| var bytesWritten = MD5.HashData(data.AsSpan(), destination.AsSpan()); | ||
|
|
||
| // Assert | ||
| bytesWritten.Should().Be(16); | ||
| destination.Should().Equal(ExpectedHash); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void HashData_Span_WithDestination_TooSmall_Test() | ||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5 }; | ||
| var destination = new byte[8]; | ||
|
|
||
| // Act & assert | ||
| Assert.Throws<ArgumentException>(() => MD5.HashData(data.AsSpan(), destination.AsSpan())); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryHashData_Test() | ||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5 }; | ||
| var destination = new byte[16]; | ||
|
|
||
| // Act | ||
| var result = MD5.TryHashData(data.AsSpan(), destination.AsSpan(), out var bytesWritten); | ||
|
|
||
| // Assert | ||
| result.Should().BeTrue(); | ||
| bytesWritten.Should().Be(16); | ||
| destination.Should().Equal(ExpectedHash); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryHashData_TooSmall_Test() | ||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5 }; | ||
| var destination = new byte[8]; | ||
|
|
||
| // Act | ||
| var result = MD5.TryHashData(data.AsSpan(), destination.AsSpan(), out var bytesWritten); | ||
|
|
||
| // Assert | ||
| result.Should().BeFalse(); | ||
| bytesWritten.Should().Be(0); | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| using System; | ||
| using System.Security.Cryptography; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PolyShim.Tests.Net50; | ||
|
|
||
| public class SHA1Tests | ||
| { | ||
| // SHA1([1, 2, 3, 4, 5]) | ||
| private static readonly byte[] ExpectedHash = | ||
| [ | ||
| 0x11, | ||
| 0x96, | ||
| 0x6a, | ||
| 0xb9, | ||
| 0xc0, | ||
| 0x99, | ||
| 0xf8, | ||
| 0xfa, | ||
| 0xbe, | ||
| 0xfa, | ||
| 0xc5, | ||
| 0x4c, | ||
| 0x08, | ||
| 0xd5, | ||
| 0xbe, | ||
| 0x2b, | ||
| 0xd8, | ||
| 0xc9, | ||
| 0x03, | ||
| 0xaf, | ||
| ]; | ||
|
|
||
| [Fact] | ||
| public void HashData_Array_Test() | ||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5 }; | ||
|
|
||
| // Act | ||
| var hash = SHA1.HashData(data); | ||
|
|
||
| // Assert | ||
| hash.Should().Equal(ExpectedHash); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void HashData_Span_Test() | ||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5 }; | ||
|
|
||
| // Act | ||
| var hash = SHA1.HashData(data.AsSpan()); | ||
|
|
||
| // Assert | ||
| hash.Should().Equal(ExpectedHash); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void HashData_Span_WithDestination_Test() | ||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5 }; | ||
| var destination = new byte[20]; | ||
|
|
||
| // Act | ||
| var bytesWritten = SHA1.HashData(data.AsSpan(), destination.AsSpan()); | ||
|
|
||
| // Assert | ||
| bytesWritten.Should().Be(20); | ||
| destination.Should().Equal(ExpectedHash); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void HashData_Span_WithDestination_TooSmall_Test() | ||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5 }; | ||
| var destination = new byte[10]; | ||
|
|
||
| // Act & assert | ||
| Assert.Throws<ArgumentException>(() => SHA1.HashData(data.AsSpan(), destination.AsSpan())); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryHashData_Test() | ||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5 }; | ||
| var destination = new byte[20]; | ||
|
|
||
| // Act | ||
| var result = SHA1.TryHashData(data.AsSpan(), destination.AsSpan(), out var bytesWritten); | ||
|
|
||
| // Assert | ||
| result.Should().BeTrue(); | ||
| bytesWritten.Should().Be(20); | ||
| destination.Should().Equal(ExpectedHash); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryHashData_TooSmall_Test() | ||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5 }; | ||
| var destination = new byte[10]; | ||
|
|
||
| // Act | ||
| var result = SHA1.TryHashData(data.AsSpan(), destination.AsSpan(), out var bytesWritten); | ||
|
|
||
| // Assert | ||
| result.Should().BeFalse(); | ||
| bytesWritten.Should().Be(0); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| using System; | ||
| using System.Security.Cryptography; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PolyShim.Tests.Net50; | ||
|
|
||
| public class SHA256Tests | ||
| { | ||
| // SHA256([1, 2, 3, 4, 5]) | ||
| private static readonly byte[] ExpectedHash = | ||
| [ | ||
| 0x74, | ||
| 0xf8, | ||
| 0x1f, | ||
| 0xe1, | ||
| 0x67, | ||
| 0xd9, | ||
| 0x9b, | ||
| 0x4c, | ||
| 0xb4, | ||
| 0x1d, | ||
| 0x6d, | ||
| 0x0c, | ||
| 0xcd, | ||
| 0xa8, | ||
| 0x22, | ||
| 0x78, | ||
| 0xca, | ||
| 0xee, | ||
| 0x9f, | ||
| 0x3e, | ||
| 0x2f, | ||
| 0x25, | ||
| 0xd5, | ||
| 0xe5, | ||
| 0xa3, | ||
| 0x93, | ||
| 0x6f, | ||
| 0xf3, | ||
| 0xdc, | ||
| 0xec, | ||
| 0x60, | ||
| 0xd0, | ||
| ]; | ||
|
|
||
| [Fact] | ||
| public void HashData_Array_Test() | ||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5 }; | ||
|
|
||
| // Act | ||
| var hash = SHA256.HashData(data); | ||
|
|
||
| // Assert | ||
| hash.Should().Equal(ExpectedHash); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void HashData_Span_Test() | ||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5 }; | ||
|
|
||
| // Act | ||
| var hash = SHA256.HashData(data.AsSpan()); | ||
|
|
||
| // Assert | ||
| hash.Should().Equal(ExpectedHash); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void HashData_Span_WithDestination_Test() | ||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5 }; | ||
| var destination = new byte[32]; | ||
|
|
||
| // Act | ||
| var bytesWritten = SHA256.HashData(data.AsSpan(), destination.AsSpan()); | ||
|
|
||
| // Assert | ||
| bytesWritten.Should().Be(32); | ||
| destination.Should().Equal(ExpectedHash); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void HashData_Span_WithDestination_TooSmall_Test() | ||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5 }; | ||
| var destination = new byte[16]; | ||
|
|
||
| // Act & assert | ||
| Assert.Throws<ArgumentException>(() => | ||
| SHA256.HashData(data.AsSpan(), destination.AsSpan()) | ||
| ); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryHashData_Test() | ||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5 }; | ||
| var destination = new byte[32]; | ||
|
|
||
| // Act | ||
| var result = SHA256.TryHashData(data.AsSpan(), destination.AsSpan(), out var bytesWritten); | ||
|
|
||
| // Assert | ||
| result.Should().BeTrue(); | ||
| bytesWritten.Should().Be(32); | ||
| destination.Should().Equal(ExpectedHash); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryHashData_TooSmall_Test() | ||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5 }; | ||
| var destination = new byte[16]; | ||
|
|
||
| // Act | ||
| var result = SHA256.TryHashData(data.AsSpan(), destination.AsSpan(), out var bytesWritten); | ||
|
|
||
| // Assert | ||
| result.Should().BeFalse(); | ||
| bytesWritten.Should().Be(0); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.