Skip to content
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

Mark RNGCryptoServiceProvider as Obsolete #52373

Merged
merged 8 commits into from
May 8, 2021
1 change: 1 addition & 0 deletions docs/project/list-of-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ The PR that reveals the implementation of the `<IncludeInternalObsoleteAttribute
| __`SYSLIB0019`__ | RuntimeEnvironment members SystemConfigurationFile, GetRuntimeInterfaceAsIntPtr, and GetRuntimeInterfaceAsObject are no longer supported and throw PlatformNotSupportedException. |
| __`SYSLIB0020`__ | JsonSerializerOptions.IgnoreNullValues is obsolete. To ignore null values when serializing, set DefaultIgnoreCondition to JsonIgnoreCondition.WhenWritingNull. |
| __`SYSLIB0022`__ | The Rijndael and RijndaelManaged types are obsolete. Use Aes instead. |
| __`SYSLIB0023`__ | RNGCryptoServiceProvider is obsolete. Use RandomNumberGenerator.Create() instead. |
jeffhandley marked this conversation as resolved.
Show resolved Hide resolved

## Analyzer Warnings

Expand Down
3 changes: 3 additions & 0 deletions src/libraries/Common/src/System/Obsoletions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,8 @@ internal static class Obsoletions

internal const string RijndaelMessage = "The Rijndael and RijndaelManaged types are obsolete. Use Aes instead.";
internal const string RijndaelDiagId = "SYSLIB0022";

internal const string RNGCryptoServiceProviderMessage = "RNGCryptoServiceProvider is obsolete. Use RandomNumberGenerator.Create() instead.";
internal const string RNGCryptoServiceProviderDiagId = "SYSLIB0023";
}
}
3 changes: 2 additions & 1 deletion src/libraries/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
SYSLIB0004: Constrained Execution Region (CER).
SYSLIB0017: Strong name signing.
SYSLIB0022: Rijndael types.
SYSLIB0023: RNGCryptoServiceProvider.
-->
<NoWarn Condition="'$(IsPartialFacadeAssembly)' == 'true'">$(NoWarn);SYSLIB0003;SYSLIB0004;SYSLIB0015;SYSLIB0017;SYSLIB0022</NoWarn>
<NoWarn Condition="'$(IsPartialFacadeAssembly)' == 'true'">$(NoWarn);SYSLIB0003;SYSLIB0004;SYSLIB0015;SYSLIB0017;SYSLIB0022;SYSLIB0023</NoWarn>
<!-- Reset these properties back to blank, since they are defaulted by Microsoft.NET.Sdk -->
<WarningsAsErrors Condition="'$(WarningsAsErrors)' == 'NU1605'" />
<!-- Set the documentation output file globally. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class TestAccountImpersonator : IDisposable
public TestAccountImpersonator()
{
string testAccountPassword;
using (RandomNumberGenerator rng = new RNGCryptoServiceProvider())
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
var randomBytes = new byte[33];
jeffhandley marked this conversation as resolved.
Show resolved Hide resolved
rng.GetBytes(randomBytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public RC2CryptoServiceProvider() { }
public override void GenerateIV() { }
public override void GenerateKey() { }
}
[System.ObsoleteAttribute("RNGCryptoServiceProvider is obsolete. Use RandomNumberGenerator.Create() instead.", DiagnosticId = "SYSLIB0023", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public sealed partial class RNGCryptoServiceProvider : System.Security.Cryptography.RandomNumberGenerator
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<Compile Include="Internal\Cryptography\Helpers.cs" />
<Compile Include="$(CommonPath)Internal\Cryptography\Helpers.cs"
Link="Internal\Cryptography\Helpers.cs" />
<Compile Include="$(CommonPath)System\Obsoletions.cs"
Link="Common\System\Obsoletions.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\KeySizeHelpers.cs"
Link="Common\System\Security\Cryptography\KeySizeHelpers.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\CryptoPool.cs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace System.Security.Cryptography
{
[Obsolete(Obsoletions.RNGCryptoServiceProviderMessage, DiagnosticId = Obsoletions.RNGCryptoServiceProviderDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class RNGCryptoServiceProvider : RandomNumberGenerator
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Linq;
using Xunit;

#pragma warning disable SYSLIB0023 // RNGCryptoServiceProvider is obsolete

namespace System.Security.Cryptography.RNG.Tests
{
/// <summary>
Expand Down Expand Up @@ -120,3 +122,5 @@ public static void VerifyCtors()
}
}
}

#pragma warning restore SYSLIB0023
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public WindowsTestAccount(string userName)
private void CreateUser()
{
string testAccountPassword;
using (RandomNumberGenerator rng = new RNGCryptoServiceProvider())
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
jeffhandley marked this conversation as resolved.
Show resolved Hide resolved
{
byte[] randomBytes = new byte[33];
rng.GetBytes(randomBytes);
Expand Down