‘SHA256Managed’ is obsolete: ‘Derived cryptographic types are obsolete
By FoxLearn Published on Jan 09, 2025 245
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(); } }
- Primitive types in C#
- How to set permissions for a directory in C#
- How to Convert Int to Byte Array in C#
- How to Convert string list to int list in C#
- How to convert timestamp to date in C#
- How to Get all files in a folder in C#
- How to use Channel as an async queue in C#
- Case sensitivity in JSON deserialization
Categories
Popular Posts
11 Things You Didn't Know About Cloudflare
Dec 19, 2024
AdminKit Bootstrap 5 HTML5 UI Kits Template
Nov 17, 2024
RuangAdmin Template
Nov 13, 2024
Dash UI HTML5 Admin Dashboard Template
Nov 18, 2024