[英]Is there any difference between SHA256.Create() and HashAlgorithm.Create("SHA-256")?
[英]Override default HashAlgorithm.Create()
我一直在嘗試將System.Security.Cryptography.HashAlgorithm.Create()
返回的默認值從SHA1
覆蓋到SHA256
。 基於docs ,它應該是可覆蓋的。
我看到了這篇文章,但它似乎只提到映射自定義哈希算法以覆蓋現有算法之一。 我只想將SHA1
的默認值覆蓋為SHA256
的默認值。
使用上面的文章可以嗎?
像這樣的東西?
<configuration>
<mscorlib>
<cryptographySettings>
<cryptoNameMapping>
<nameEntry name="System.Security.Cryptography.HashAlgorithm"
class="System.Security.Cryptography.SHA256"/>
</cryptoNameMapping>
</cryptographySettings>
</mscorlib>
</configuration>
是的,這是可能的。
如果您需要在機器上每個正在運行的 .NET Framework 應用程序中應用新的默認哈希算法,只需在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.config
x64 :
%windir%\Microsoft.NET\Framework64\[version]\config\machine.config
您還可以通過更改DefaultHashAlgorithm
屬性來更改每個內置哈希算法的默認算法。 請參閱此處的算法列表。
更改machine.config
的缺點是它會影響將使用System.Security.Cryptography.HashAlgorithm
所有應用程序。
另一種方法是使用CryptoConfig
類。 下一個代碼片段將SHA256Managed
注冊為默認哈希算法:
using System.Security.Cryptography;
...
CryptoConfig.AddAlgorithm(
typeof(SHA256Managed),
"System.Security.Cryptography.HashAlgorithm");
這僅更改當前應用程序的默認哈希算法。
請注意,必須使用抽象類SHA256
的具體實現SHA256Managed
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.