خطا در فایل Encoder در Visual Studio 2022

مجتبی فعال
29 بهمن ۱۴۰۰

سلام استاد

در فایل Encoder و در Visual Studio 2022 در خط زیر پیام اخطار داده می شود :

MD5 md5 = new MD5CryptoServiceProvider();

اخطار :

Warning SYSLIB0021 'MD5CryptoServiceProvider' is obsolete: 'Derived cryptographic types are obsolete. Use the Create method on the base type instead.'

 

باید چکار کنیم

تشکر

 

1122

1 پاسخ
  • محمد اشرافی30 بهمن ۱۴۰۰

    میگه این کلاس MD5CryptoServiceProvider منسوخ شده ، میتونید از اش استفاده نکنید ، ولی اگر هم استفاد کنید مشکلی نداره

    اگر بخواید می تونید از این کلاس استفاده کنید : 
     

     public static string EncodePasswordMd5(string pass) //Encrypt using MD5   
            {
                using var md5 = MD5.Create();
    
                byte[] inputBytes = Encoding.ASCII.GetBytes(pass);
                byte[] hashBytes = md5.ComputeHash(inputBytes);
    
                // Convert the byte array to hexadecimal string
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < hashBytes.Length; i++)
                {
                    sb.Append(hashBytes[i].ToString("X2"));
                }
                return sb.ToString();
            }

    یا کلا از یه الگوریتم دیگه استفاده کنید مثل sha256

     public class Sha256Encoder
        {
            public static string Encode(string inputValue)
            {
                using var sha256 = SHA256.Create();
                var originalBytes = Encoding.Default.GetBytes(inputValue);
                var encodedBytes = sha256.ComputeHash(originalBytes);
                return Convert.ToBase64String(encodedBytes);
            }
            public static bool IsCompare(string hashText, string rawText)
            {
                rawText = Encode(rawText);
                return hashText == rawText;
            }
        }