-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
API proposal: Obsolete RNGCryptoServiceProvider #40169
Comments
On the topic of |
If we really wanted, we could also have |
Hm, I'll play around with that idea. What problems / concerns arise from calling GC.SuppressFinalize repeatedly? The RNG uses that dispose pattern where
There are many other crypto classes marked as such, Does it make sense to broaden this issue to include them? |
Possibly. We could try making the changes to all callers within the dotnet/* repos before marking things as obsolete, just to see how much churn it might cause. |
Would the call to |
There are no per-instance seeds. All of our CSPRNGs use stateless functions (or, at least, functions that only use hidden state). |
@GrabYourPitchforks @terrajobst is this something what will be done for 6.0? It'd help us to limit browser crypto implementation. |
@marek-safar I don't think the timeline on marking it Obsolete helps browser. The RNGCryptoServiceProvider class is fake, it just wraps an instance of RandomNumberGenerator.Create: https://github.com/dotnet/runtime/blob/master/src/libraries/System.Security.Cryptography.Csp/src/System/Security/Cryptography/RNGCryptoServiceProvider.cs So there shouldn't be any special work for Browser to do. |
Currently, RNGCryptoServiceProvider is documented as thread-safe, while RandomNumberGenerator is not (dotnet/dotnet-api-docs#3741). And I don't think you can document
I think you should then document that RandomNumberGenerator.Create() always returns a thread-safe instance in .NET 5, but RandomNumberGenerator.Create(string) might not.
That will make it harder for developers to be sure that the field / local / parameter refers to a thread-safe instance.
On .NET Framework, |
@KalleOlaviNiemitalo The analyzer/fixer would probably only apply to libraries and applications targeting .NET 6.0 or higher, so .NET Framework concerns don't really apply. @GrabYourPitchforks Honestly, if anyone's using instances of RNGCryptoServiceProvider they should just switch to using the static methods (it's a sealed type, so there's no DRBG mocking/substitution going on, (except maybe via CspParameters ctors? but those already don't work in core)). So no local/field replacement is warranted. |
I don't think we'd document that And honestly it's just an optimization anyway. Probably a premature optimization, as if allocating RNG instances is the bottleneck in your code you're certainly doing something unique. :) If we steer devs toward the static members instead of the instance members it's all moot anyway. |
Looks good as proposed. namespace System.Security.Cryptography
{
[EditorBrowsable(EditorBrowsableState.Never)] // existing attribute
[Obsolete(DiagnosticId: nextId)] // new attribute
public sealed class RNGCryptoServiceProvider : RandomNumberGenerator
{ /* ... */ }
} |
Background and Motivation
The
RNGCryptoServiceProvider
class is a relic from the old Windows CAPI days of yore. The original Win32 API is no longer recommended, preferring CNG for all new work.In .NET Core, the
RNGCryptoServiceProvider
type is marked[EditorBrowsable(Never)]
, and the implementation ignores all provided constructor parameters and delegates to the underlying preferred OS implementation anyway.There's no reason for an application targeting .NET 6.0+ to use this API. Apps should instead use
RandomNumberGenerator.Create()
. For AOT and linker trimming scenarios, this could also help eliminate the app's dependency on the package which contains theRNGCryptoServiceProvider
type, reducing overall memory usage and disk footprint.Proposed API
This could be accompanied by a fixer with two behaviors:
RNGCryptoServiceProvider
ctors become calls to the parameterless overloadRandomNumberGenerator.Create()
.RNGCryptoServiceProvider
instead become typeRandomNumberGenerator
.The obsoletion would not affect apps targeting netstandard or .NET versions prior to 6.0, as the reference assemblies would not contain these annotations. However, the fixer could apply to all target frameworks.
The text was updated successfully, but these errors were encountered: