‘SHA256Managed’ is obsolete: ‘Derived cryptographic types are obsolete
By FoxLearn 1/9/2025 2:43:01 AM 205
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(); } }
- How to use JsonConverterFactory in C#
- How to serialize non-public properties using System.Text.Json
- The JSON value could not be converted to System.DateTime
- Try/finally with no catch block in C#
- Parsing a DateTime from a string in C#
- Async/Await with a Func delegate in C#
- How to batch read with Threading.ChannelReader in C#
- How to ignore JSON deserialization errors in C#
Categories
Popular Posts
Freedash bootstrap lite
11/13/2024
Admin BSB Free Bootstrap Admin Dashboard
11/14/2024
K-WD Tailwind CSS Admin Dashboard Template
11/17/2024
Spica Admin Dashboard Template
11/18/2024