zl程序教程

您现在的位置是:首页 >  其他

当前栏目

net6 'MD5CryptoServiceProvider' 已过时 处理方法

2023-04-18 15:03:20 时间

将项目升级到 .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); 
}