In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use the hash algorithm in .NET 6. It is very detailed and has a certain reference value. Friends who are interested must finish reading it.
Intro
Microsoft introduced some simpler API in .NET 6 to use the HMAC hash algorithm (MD5/SHA1/SHA256/SHA384/SHA512)
Microsoft is called HMAC One-Shoot method, HMAC algorithm in the ordinary hash algorithm based on the addition of a key, through key to improve security, can effectively avoid password disclosure by the rainbow table to deduce the real password, JWT (Json Web Token) in addition to the use of RSA, but also supports the use of HMAC.
New API
The new definition of API is as follows:
Namespace System.Security.Cryptography {public partial class HMACMD5 {public static byte [] HashData (byte [] key, byte [] source); public static byte [] HashData (ReadOnlySpan key, ReadOnlySpan source); public static int HashData (ReadOnlySpan key, ReadOnlySpan source, Span destination); public static bool TryHashData (ReadOnlySpan key, ReadOnlySpan source, Span destination, out int bytesWritten);} public partial class HMACSHA1 {public static byte [] HashData (byte [] key, byte [] source) Public static byte [] HashData (ReadOnlySpan key, ReadOnlySpan source); public static int HashData (ReadOnlySpan key, ReadOnlySpan source, Span destination); public static bool TryHashData (ReadOnlySpan key, ReadOnlySpan source, Span destination, out int bytesWritten);} public partial class HMACSHA256 {public static byte [] HashData (byte [] key, byte [] source); public static byte [] HashData (ReadOnlySpan key, ReadOnlySpan source); public static int HashData (ReadOnlySpan key, ReadOnlySpan source, Span destination) Public static bool TryHashData (ReadOnlySpan key, ReadOnlySpan source, Span destination, out int bytesWritten);} public partial class HMACSHA384 {public static byte [] HashData (byte [] key, byte [] source); public static byte [] HashData (ReadOnlySpan key, ReadOnlySpan source); public static int HashData (ReadOnlySpan key, ReadOnlySpan source, Span destination); public static bool TryHashData (ReadOnlySpan key, ReadOnlySpan source, Span destination, out int bytesWritten) } public partial class HMACSHA512 {public static byte [] HashData (byte [] key, byte [] source); public static byte [] HashData (ReadOnlySpan key, ReadOnlySpan source); public static int HashData (ReadOnlySpan key, ReadOnlySpan source, Span destination); public static bool TryHashData (ReadOnlySpan key, ReadOnlySpan source, Span destination, out int bytesWritten);} Sample Before
In the previous version, it would be complicated to implement the HMAC algorithm. A HashHelper was implemented to encapsulate the commonly used Hash algorithm and HMAC algorithm. Part of the HashHelper code is as follows. The complete code can be obtained from Github: https://github.com/WeihanLi/WeihanLi.Common/blob/dev/src/WeihanLi.Common/Helpers/HashHelper.cs.
/ get the hash string / hash type / / source / key / is the string public static string GetHashedString (HashType type, byte [] source, byte []) processed by the lowercase hash algorithm? Key, bool isLower = false) {Guard.NotNull (source, nameof (source)); if (source.Length = = 0) {return string.Empty;} var hashedBytes = GetHashedBytes (type, source, key); var sbText = new StringBuilder (); if (isLower) {foreach (var b in hashedBytes) {sbText.Append (b.ToString ("x2")) } else {foreach (var b in hashedBytes) {sbText.Append (b.ToString ("X2"));}} return sbText.ToString () } / calculate string hash value / hash type / string to hash / Byte array over hash public static byte [] GetHashedBytes (HashType type, string str) = > GetHashedBytes (type, str, Encoding.UTF8) / calculate string hash value / hash type / string to be hash / / Encoding type / byte array public static byte [] GetHashedBytes (HashType type, string str, Encoding encoding) {Guard.NotNull (str, nameof (str)); if (str = = string.Empty) {return Array.Empty ();} var bytes = encoding.GetBytes (str) Return GetHashedBytes (type, bytes);} / get the byte array after Hash / hash type / original byte array / public static byte [] GetHashedBytes (HashType type, byte [] bytes) = > GetHashedBytes (type, bytes, null) / get the byte array after Hash / key / the original byte array / public static byte [] GetHashedBytes (HashType type, byte [] bytes, byte []? Key) {Guard.NotNull (bytes, nameof (bytes)); if (bytes.Length = = 0) {return bytes;} HashAlgorithm algorithm = null! Try {if (key = = null) {algorithm = type switch {HashType.SHA1 = > new SHA1Managed (), HashType.SHA256 = > new SHA256Managed (), HashType.SHA384 = > new SHA384Managed (), HashType.SHA512 = > new SHA512Managed () _ = > MD5.Create ()} } else {algorithm = type switch {HashType.SHA1 = > new HMACSHA1 (key), HashType.SHA256 = > new HMACSHA256 (key), HashType.SHA384 = > new HMACSHA384 (key), HashType.SHA512 = > new HMACSHA512 (key) _ = > new HMACMD5 (key)} } return algorithm.ComputeHash (bytes);} finally {algorithm.Dispose ();}}
Examples of use are as follows:
HashHelper.GetHashedBytes (HashType.MD5, "test"); HashHelper.GetHashedBytes (HashType.MD5, "test" .GetBytes ()); HashHelper.GetHashedBytes (HashType.MD5, "test", "testKey"); HashHelper.GetHashedBytes (HashType.MD5, "test" .GetBytes (), "testKey" .GetBytes ()); HashHelper.GetHashedString (HashType.MD5, "test"); HashHelper.GetHashedString (HashType.SHA1, "test" .GetBytes ()); HashHelper.GetHashedString (HashType.SHA256, "test", "testKey") HashHelper.GetHashedString (HashType.MD5, "test" .GetBytes (), "testKey" .GetBytes ()); New API Sample
How can you simplify with the new API? let's take a look at the following example:
Var bytes = "test" .GetBytes (); var keyBytes = "test-key" .GetBytes (); / / HMACMD5 var hmd5V1 = HMACMD5.HashData (keyBytes, bytes); var hmd5V2 = HashHelper.GetHashedBytes (HashType.MD5, bytes, keyBytes); Console.WriteLine (hmd5V2.SequenceEqual (hmd5V1)); / / HMACSHA1 var hsha1V1 = HMACSHA1.HashData (keyBytes, bytes); var hsha1V2 = HashHelper.GetHashedBytes (HashType.SHA1, bytes, keyBytes); Console.WriteLine (hsha1V2.SequenceEqual (hsha1V1)); / / HMACSHA256 var hsha256V1 = HMACSHA256.HashData (keyBytes, bytes) Var hsha256V2 = HashHelper.GetHashedBytes (HashType.SHA256, bytes, keyBytes); Console.WriteLine (hsha256V2.SequenceEqual (hsha256V1)); / / HMACSHA384 var hsha384V1 = HMACSHA384.HashData (keyBytes, bytes); var hsha384V2 = HashHelper.GetHashedBytes (HashType.SHA384, bytes, keyBytes); Console.WriteLine (hsha384V2.SequenceEqual (hsha384V1)); / / HMACSHA512 var hsha512V1 = HMACSHA512.HashData (keyBytes, bytes); var hsha512V2 = HashHelper.GetHashedBytes (HashType.SHA512, bytes, keyBytes); Console.WriteLine (hsha512V2.SequenceEqual (hsha512V1))
You can directly use the HashData method of the corresponding HMAC hash algorithm, and input the corresponding key and the original content. The above is compared with our HashHelper encapsulated method to see whether the results are consistent. The output results are as follows:
More
For ordinary hashing algorithms, Microsoft has already supported the above usage in .NET 5, so you can try the following code:
Var bytes = "test" .GetBytes (); / / MD5 var md5V1 = MD5.HashData (bytes); var md5V2 = HashHelper.GetHashedBytes (HashType.MD5, bytes); Console.WriteLine (md5V2.SequenceEqual (md5V1)); / / SHA1 var sha1V1 = SHA1.HashData (bytes); var sha1V2 = HashHelper.GetHashedBytes (HashType.SHA1, bytes); Console.WriteLine (sha1V2.SequenceEqual (sha1V1)); / / SHA256 var sha256V1 = SHA256.HashData (bytes); var sha256V2 = HashHelper.GetHashedBytes (HashType.SHA256, bytes); Console.WriteLine (sha256V2.SequenceEqual (sha256V1)) / / SHA384 var sha384V1 = SHA384.HashData (bytes); var sha384V2 = HashHelper.GetHashedBytes (HashType.SHA384, bytes); Console.WriteLine (sha384V2.SequenceEqual (sha384V1)); / / SHA512 var sha512V1 = SHA512.HashData (bytes); var sha512V2 = HashHelper.GetHashedBytes (HashType.SHA512, bytes); Console.WriteLine (sha512V2.SequenceEqual (sha512V1))
Most of the time, we may have to use the string after MD5 or SHA1. I don't know why Microsoft doesn't have a method to get a string directly. If there is such a method, it will be more convenient. Compared with later, it is more comfortable to use self-encapsulated HashHelper. Haha, this static method is not abstract enough. If you want to replace the hash algorithm code dynamically, it may be a little bit.
The above is all the contents of the article "how to use the hash algorithm in .NET 6". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.