‘SHA256Managed’ is obsolete: ‘Derived cryptographic types are obsolete
By Tan Lee Published on Jan 09, 2025 639
Warning SYSLIB0021 'SHA512Managed' is obsolete: 'Derived cryptographic types are obsolete. Use the Create method on the base type instead.'
The code that triggers this warning looks like this:
using System.Text; using System.Security.Cryptography; public static string ToSHA512(string s) { SHA512Managed sha512 = new SHA512Managed(); StringBuilder hash = new StringBuilder(); byte[] hashArray = sha512.ComputeHash(Encoding.UTF8.GetBytes(s)); foreach (byte b in hashArray) { hash.Append(b.ToString("x")); } return hash.ToString(); }
This warning appears because the classes SHA256Managed
, SHA512Managed
, SHA1Managed
, and SHA384Managed
have been deprecated. These are now replaced with SHA256
, SHA512
, SHA1
, and SHA384
, respectively.
To resolve the warning, you should replace the SHA512Managed
class with its updated version.
using System.Text; using System.Security.Cryptography; public static string ToSHA512(string s) { using (SHA512 sha512 = SHA512.Create()) { StringBuilder hash = new StringBuilder(); byte[] hashArray = sha512.ComputeHash(Encoding.UTF8.GetBytes(s)); foreach (byte b in hashArray) { hash.Append(b.ToString("x")); } return hash.ToString(); } }
Both versions (the deprecated and the updated one) will return the same result.
// Deprecated version, do not use in .NET Core 6 and newer private static string SHA1_Old(string s) { SHA1Managed sha1 = new SHA1Managed(); StringBuilder hash = new StringBuilder(); byte[] hashArray = sha1.ComputeHash(Encoding.UTF8.GetBytes(s)); foreach (byte b in hashArray) { hash.Append(b.ToString("x")); } return hash.ToString(); } // New version for .NET Core 6 and newer private static string SHA1_New(string s) { using (SHA1 sha1 = SHA1.Create()) { StringBuilder hash = new StringBuilder(); byte[] hashArray = sha1.ComputeHash(Encoding.UTF8.GetBytes(s)); foreach (byte b in hashArray) { hash.Append(b.ToString("x")); } return hash.ToString(); } }
Categories
Popular Posts
Structured Data using FoxLearn.JsonLd
Jun 20, 2025
Implement security headers for an ASP.NET Core
Jun 24, 2025
Star Admin Dashboard Template
Nov 14, 2024
Freedash bootstrap lite
Nov 13, 2024