Maui Blazor 中文社区 QQ群:645660665

net6 'MD5CryptoServiceProvider' 已过时 处理方法

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

posted @ 2022-10-25 19:49  AlexChow  阅读(3128)  评论(0)    收藏  举报