将项目升级到 .NET 6 后,编译器开始抱怨以下警告消息:
warning SYSLIB0021: “MD5CryptoServiceProvider”已过时:“Derived cryptographic types are obsolete. Use the Create method on the base type instead.”
这是导致此警告的代码:
public static string MD5Crypto(string key)
{
byte[] hash = (new ASCIIEncoding()).GetBytes(key);
using (var md5 = MD5CryptoServiceProvider())
hash = md5.ComputeHash(hash);
return (new ASCIIEncoding()).GetString(hash);
}
修复很简单:
public static string MD5Crypto(string key)
{
byte[] hash = (new ASCIIEncoding()).GetBytes(key);
using (var md5 = MD5.Create())
hash = md5.ComputeHash(hash);
return (new ASCIIEncoding()).GetString(hash);
}
标签:过时,ASCIIEncoding,hash,string,MD5CryptoServiceProvider,key,new,net6,md5
From: https://www.cnblogs.com/densen2014/p/16826092.html