-
Notifications
You must be signed in to change notification settings - Fork 5
Add polyfills for RandomNumberGenerator.Fill(...)
#73
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| using System; | ||
| using System.Security.Cryptography; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PolyShim.Tests.NetCore21; | ||
|
|
||
| public class RandomNumberGeneratorTests | ||
| { | ||
| [Fact] | ||
| public void Fill_Span_Test() | ||
| { | ||
| // Arrange | ||
| Span<byte> buffer = stackalloc byte[16]; | ||
|
|
||
| // Act | ||
| RandomNumberGenerator.Fill(buffer); | ||
|
|
||
| // Assert | ||
| // Since cryptographic random data is non-deterministic, | ||
| // we can only verify that the buffer contains some non-zero bytes | ||
| buffer.ToArray().Should().Contain(b => b != 0); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #if (NETCOREAPP && !NETCOREAPP2_1_OR_GREATER) || (NETFRAMEWORK && NET45_OR_GREATER) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is missing net standard boundaries (up to 2.1)
|
||
| #nullable enable | ||
| // ReSharper disable RedundantUsingDirective | ||
| // ReSharper disable CheckNamespace | ||
| // ReSharper disable InconsistentNaming | ||
| // ReSharper disable PartialTypeWithSinglePart | ||
|
|
||
| using System; | ||
| using System.Security.Cryptography; | ||
|
|
||
| internal static partial class PolyfillExtensions | ||
| { | ||
| extension(RandomNumberGenerator) | ||
| { | ||
| // https://learn.microsoft.com/dotnet/api/system.security.cryptography.randomnumbergenerator.fill | ||
| public static void Fill(Span<byte> data) | ||
| { | ||
| var buffer = new byte[data.Length]; | ||
| using var rng = RandomNumberGenerator.Create(); | ||
| rng.GetBytes(buffer); | ||
| buffer.CopyTo(data); | ||
|
Comment on lines
+16
to
+21
|
||
| } | ||
| } | ||
| } | ||
| #endif | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test uses
stackalloc byte[16], which is not guaranteed to be zero-initialized; the assertion can pass even ifRandomNumberGenerator.Fill(...)is a no-op because the stack buffer may already contain non-zero bytes. Initialize the span (e.g., callbuffer.Clear()or use anew byte[16]) before invokingFillso the test actually validates that bytes were written.