Skip to content

Commit ebd695f

Browse files
Mark RNGCryptoServiceProvider as Obsolete (#52373)
* Mark RNGCryptoServiceProvider as Obsolete * Use the static methods on RandomNumberGenerator instead of Create() * Use RandomNumberGenerator.GetBytes() instead of RandomNumberGenerator.Create().GetBytes() * Revert "Use RandomNumberGenerator.GetBytes() instead of RandomNumberGenerator.Create().GetBytes()" This reverts commit 447f848. * Update src/libraries/System.IO.Pipes/tests/NamedPipeTests/NamedPipeTest.CurrentUserOnly.Windows.cs Co-authored-by: Jeremy Barton <jbarton@microsoft.com>
1 parent db85f31 commit ebd695f

File tree

9 files changed

+18
-9
lines changed

9 files changed

+18
-9
lines changed

docs/project/list-of-diagnostics.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ The PR that reveals the implementation of the `<IncludeInternalObsoleteAttribute
7676
| __`SYSLIB0019`__ | RuntimeEnvironment members SystemConfigurationFile, GetRuntimeInterfaceAsIntPtr, and GetRuntimeInterfaceAsObject are no longer supported and throw PlatformNotSupportedException. |
7777
| __`SYSLIB0020`__ | JsonSerializerOptions.IgnoreNullValues is obsolete. To ignore null values when serializing, set DefaultIgnoreCondition to JsonIgnoreCondition.WhenWritingNull. |
7878
| __`SYSLIB0022`__ | The Rijndael and RijndaelManaged types are obsolete. Use Aes instead. |
79+
| __`SYSLIB0023`__ | RNGCryptoServiceProvider is obsolete. To generate a random number, use one of the RandomNumberGenerator static methods instead. |
7980

8081
## Analyzer Warnings
8182

src/libraries/Common/src/System/Obsoletions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,8 @@ internal static class Obsoletions
7474

7575
internal const string RijndaelMessage = "The Rijndael and RijndaelManaged types are obsolete. Use Aes instead.";
7676
internal const string RijndaelDiagId = "SYSLIB0022";
77+
78+
internal const string RNGCryptoServiceProviderMessage = "RNGCryptoServiceProvider is obsolete. To generate a random number, use one of the RandomNumberGenerator static methods instead.";
79+
internal const string RNGCryptoServiceProviderDiagId = "SYSLIB0023";
7780
}
7881
}

src/libraries/Directory.Build.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
SYSLIB0004: Constrained Execution Region (CER).
2222
SYSLIB0017: Strong name signing.
2323
SYSLIB0022: Rijndael types.
24+
SYSLIB0023: RNGCryptoServiceProvider.
2425
-->
25-
<NoWarn Condition="'$(IsPartialFacadeAssembly)' == 'true'">$(NoWarn);SYSLIB0003;SYSLIB0004;SYSLIB0015;SYSLIB0017;SYSLIB0022</NoWarn>
26+
<NoWarn Condition="'$(IsPartialFacadeAssembly)' == 'true'">$(NoWarn);SYSLIB0003;SYSLIB0004;SYSLIB0015;SYSLIB0017;SYSLIB0022;SYSLIB0023</NoWarn>
2627
<!-- Reset these properties back to blank, since they are defaulted by Microsoft.NET.Sdk -->
2728
<WarningsAsErrors Condition="'$(WarningsAsErrors)' == 'NU1605'" />
2829
<!-- Set the documentation output file globally. -->

src/libraries/System.IO.Pipes/tests/NamedPipeTests/NamedPipeTest.CurrentUserOnly.Windows.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,10 @@ public class TestAccountImpersonator : IDisposable
2222
public TestAccountImpersonator()
2323
{
2424
string testAccountPassword;
25-
using (RandomNumberGenerator rng = new RNGCryptoServiceProvider())
26-
{
27-
var randomBytes = new byte[33];
28-
rng.GetBytes(randomBytes);
25+
byte[] randomBytes = RandomNumberGenerator.GetBytes(33);
2926

30-
// Add special chars to ensure it satisfies password requirements.
31-
testAccountPassword = Convert.ToBase64String(randomBytes) + "_-As@!%*(1)4#2";
32-
}
27+
// Add special chars to ensure it satisfies password requirements.
28+
testAccountPassword = Convert.ToBase64String(randomBytes) + "_-As@!%*(1)4#2";
3329

3430
DateTime accountExpirationDate = DateTime.UtcNow + TimeSpan.FromMinutes(2);
3531
using (var principalCtx = new PrincipalContext(ContextType.Machine))

src/libraries/System.Security.Cryptography.Csp/ref/System.Security.Cryptography.Csp.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ public RC2CryptoServiceProvider() { }
175175
public override void GenerateIV() { }
176176
public override void GenerateKey() { }
177177
}
178+
[System.ObsoleteAttribute("RNGCryptoServiceProvider is obsolete. To generate a random number, use one of the RandomNumberGenerator static methods instead.", DiagnosticId = "SYSLIB0023", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
178179
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
179180
public sealed partial class RNGCryptoServiceProvider : System.Security.Cryptography.RandomNumberGenerator
180181
{

src/libraries/System.Security.Cryptography.Csp/src/System.Security.Cryptography.Csp.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
<Compile Include="Internal\Cryptography\Helpers.cs" />
2828
<Compile Include="$(CommonPath)Internal\Cryptography\Helpers.cs"
2929
Link="Internal\Cryptography\Helpers.cs" />
30+
<Compile Include="$(CommonPath)System\Obsoletions.cs"
31+
Link="Common\System\Obsoletions.cs" />
3032
<Compile Include="$(CommonPath)System\Security\Cryptography\KeySizeHelpers.cs"
3133
Link="Common\System\Security\Cryptography\KeySizeHelpers.cs" />
3234
<Compile Include="$(CommonPath)System\Security\Cryptography\CryptoPool.cs"

src/libraries/System.Security.Cryptography.Csp/src/System/Security/Cryptography/RNGCryptoServiceProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace System.Security.Cryptography
77
{
8+
[Obsolete(Obsoletions.RNGCryptoServiceProviderMessage, DiagnosticId = Obsoletions.RNGCryptoServiceProviderDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
89
[EditorBrowsable(EditorBrowsableState.Never)]
910
public sealed class RNGCryptoServiceProvider : RandomNumberGenerator
1011
{

src/libraries/System.Security.Cryptography.Csp/tests/RNGCryptoServiceProviderTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.Linq;
55
using Xunit;
66

7+
#pragma warning disable SYSLIB0023 // RNGCryptoServiceProvider is obsolete
8+
79
namespace System.Security.Cryptography.RNG.Tests
810
{
911
/// <summary>
@@ -120,3 +122,5 @@ public static void VerifyCtors()
120122
}
121123
}
122124
}
125+
126+
#pragma warning restore SYSLIB0023

src/libraries/System.Security.Principal.Windows/tests/WindowsIdentityImpersonatedTests.netcoreapp.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public WindowsTestAccount(string userName)
128128
private void CreateUser()
129129
{
130130
string testAccountPassword;
131-
using (RandomNumberGenerator rng = new RNGCryptoServiceProvider())
131+
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
132132
{
133133
byte[] randomBytes = new byte[33];
134134
rng.GetBytes(randomBytes);

0 commit comments

Comments
 (0)