我一直试图覆盖默认值从SHA1到SHA256的System.Security.Cryptography.HashAlgorithm.Create()返回的内容。基于文档,它应该是可重写的。
我遇到了这个文章,但它似乎只提到映射一个自定义哈希算法来覆盖现有的一个哈希算法。我只想将SHA1的默认值重写为默认的SHA256。
用上面的文章可以吗?
像这样吗?
<configuration>
<mscorlib>
<cryptographySettings>
<cryptoNameMapping>
<nameEntry name="System.Security.Cryptography.HashAlgorithm"
class="System.Security.Cryptography.SHA256"/>
</cryptoNameMapping>
</cryptographySettings>
</mscorlib>
</configuration>发布于 2020-04-01 11:19:08
是的,,有可能。
如果需要在机器上运行的每个.NET框架应用程序中应用新的默认哈希算法,只需在machine.config文件中写入本节:
<mscorlib>
<cryptographySettings>
<cryptoNameMapping>
<cryptoClasses>
<cryptoClass DefaultHashAlgorithm="System.Security.Cryptography.SHA256Managed, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</cryptoClasses>
<nameEntry name="System.Security.Cryptography.HashAlgorithm"
class="DefaultHashAlgorithm"/>
</cryptoNameMapping>
</cryptographySettings>
</mscorlib> 请注意,放置在这里的machine.config文件:
x32
%windir%\Microsoft.NET\Framework\[version]\config\machine.configx64
%windir%\Microsoft.NET\Framework64\[version]\config\machine.config 此外,您还可以通过更改DefaultHashAlgorithm属性来更改每个内置哈希算法的默认算法。参见算法列表这里。
发布于 2020-04-01 11:33:28
更改machine.config的缺点是它会影响所有使用System.Security.Cryptography.HashAlgorithm的应用程序。
另一种方法是使用CryptoConfig类。下一个代码段将SHA256Managed注册为默认哈希算法:
using System.Security.Cryptography;
...
CryptoConfig.AddAlgorithm(
typeof(SHA256Managed),
"System.Security.Cryptography.HashAlgorithm");这仅更改当前应用程序的默认哈希算法。
注意到,必须使用抽象类SHA256的具体实现SHA256Managed。
https://stackoverflow.com/questions/60962252
复制相似问题