Repository navigation
Make HashAlgorithm.Create(string) and friends work on .NET Core - #28385
Conversation
The Create(void) methods will continue to throw PNSE, because we do not want people depending on the "I'll create a thing for you" behavior and assuming that it has positive value (historically it has been kept as-is in .NET Framework for compatibility sake). Now they have a message in their PNSE. The Create(string) methods will forward to CryptoConfig, just like in .NET Framework. The difference in .NET Core is that CryptoConfig lives in a future assembly, and rather than build System.Private.Cryptography for this, just build a reflection bridge.
|
Asking for "ECDsa" doesn't work on non-Windows because it's CNG-backed by default. I'll think on whether I want to remove the test case, or make CryptoConfig do something differently on those systems. |
| } | ||
|
|
||
| s_createFromName = (Func<string, object>)createFromName.CreateDelegate(typeof(Func<string, object>)); | ||
| } |
There was a problem hiding this comment.
Could you make this into a static method that returns the func and then use that in the declaration of the field? Then the type can be marked beforefieldinit by the compiler.
|
|
||
| const string CreateFromNameMethodName = "CreateFromName"; | ||
|
|
||
| Type t = Type.GetType(CryptoConfigTypeName, true); |
There was a problem hiding this comment.
Nit: could you name the bool argument so it's obvious what it is? IIRC, it's throwOnError?
There was a problem hiding this comment.
I would if it'd be better to make it throwOnError:false, and then if you get back null, make s_createFromName be a delegate that throws a PlatformNotSupportedException.
There was a problem hiding this comment.
What's the motivation for the PNSE? The type would only be missing if 1) we moved it (which we'd notice via the tests) or 2) a trimmer removed it. If a trimmer removed it then the TypeLoadException seems more informative.
Of course, it could be a PNSE which carries the TypeLoadException or MissingMethodException as an innerException.
| { | ||
| // These are not supported because CryptoConfig exists in Algorithms assembly. | ||
| // CryptoConfig exists in Algorithms partly because it requires the Oid class in Encoding assembly. | ||
| // .NET Core does not allow the base classes to pick an algorithm. |
There was a problem hiding this comment.
And we still want to stick with that behavior?
There was a problem hiding this comment.
Yeah. HashAlgorithm.Create() was built as being SHA-1, because SHA-1 was the best algorithm available at the time. Now SHA-1 is considered broken, but our normal compat rules say that we can't change it for Framework, because a number of people could be
a) blind casting it to SHA1CryptoServiceProvider
b) Depending on the output value without checking to see what algorithm was even used.
The only way to use it correctly would be if you call it before producing a new hash, then you inspect the return value and write down an identifier which will get you to tell the hash recipient (you or a remote party) which algorithm was used so they can get the right one later. So the too-much-of-a-base-class Create() methods are pits of failure. With slick walls. And spikes at the bottom. If bubonic plague can survive in stagnant water it is probably in this pit.
GrabYourPitchforks
left a comment
There was a problem hiding this comment.
Only minor feedback, not critical.
| <value>FlushFinalBlock() method was called twice on a CryptoStream. It can only be called once.</value> | ||
| </data> | ||
| <data name="Cryptography_DefaultAlgorithm_NotSupported" xml:space="preserve"> | ||
| <value>This platform does not allow the automatic selection of an algorithm.</value> |
There was a problem hiding this comment.
Nit: I almost feel like we'll want an fwlink in here to direct developers to documentation showing the correct usage pattern.
| { | ||
| return Create("System.Security.Cryptography.AsymmetricAlgorithm"); | ||
| } | ||
| public static AsymmetricAlgorithm Create() => |
There was a problem hiding this comment.
Is it possible to add [Obsolete] (as error) to the reference assemblies so that devs targeting netstandard or netcoreapp will see the failure at compilation time?
There was a problem hiding this comment.
Last I knew: no (against our rules). But we were/did/will build(ing) an analyzer pack of methods that are cross-platform risks and/or just generally disrecommended. Rfc2898DeriveBytes overloads which don't take the HashAlgorithmName are on it, as are all of these.
|
@dotnet-bot Test Windows x86 Release Build please (System.Data.Tests.DataTableTest4/XmlTest13 failed with System.UnauthorizedAccessException : Access to the path 'C:\Users\DotNetTestRunner\AppData\Local' is denied.) |
…et/corefx#28385) * Make HashAlgorithm.Create(string) and friends work on .NET Core The Create(void) methods will continue to throw PNSE, because we do not want people depending on the "I'll create a thing for you" behavior and assuming that it has positive value (historically it has been kept as-is in .NET Framework for compatibility sake). Now they have a message in their PNSE. The Create(string) methods will forward to CryptoConfig, just like in .NET Framework. The difference in .NET Core is that CryptoConfig lives in a future assembly, and rather than build System.Private.Cryptography for this, just build a reflection bridge. * Review feedback * Make CryptoConfig.CreateFromName("ECDsa") work on non-Windows * Make the CryptoConfig tests aware that "ECDsa" works on non-Windows now Commit migrated from dotnet/corefx@57608f6
The Create(void) methods will continue to throw PNSE, because we do not want
people depending on the "I'll create a thing for you" behavior and assuming that
it has positive value (historically it has been kept as-is in .NET Framework for
compatibility sake). Now they have a message in their PNSE.
The Create(string) methods will forward to CryptoConfig, just like in .NET Framework.
The difference in .NET Core is that CryptoConfig lives in a future assembly, and
rather than build System.Private.Cryptography for this, just build a reflection bridge.
Fixes #22626.