using System.Security.Cryptography; byte[] key = Encoding.UTF8.GetBytes("secretKey"); byte[] message = Encoding.UTF8.GetBytes("Hello world"); using (var hmac = new HMACSHA256(key)) { byte[] hash = hmac.ComputeHash(message); Console.WriteLine(BitConverter.ToString(hash).Replace("-", "")); }
using System.Security.Cryptography; var hmac = new HMACSHA256(); using (var rng = new RNGCryptoServiceProvider()) { byte[] key = new byte[32]; rng.GetBytes(key); hmac.Key = key; } byte[] message = Encoding.UTF8.GetBytes("Hello world"); byte[] hash = hmac.ComputeHash(message); Console.WriteLine(BitConverter.ToString(hash).Replace("-", ""));In this code example, a random 32-byte key is generated using the RNGCryptoServiceProvider. The key is then used to create an HMACSHA256 instance that can be used to compute the hash of a message. The resulting hash is again displayed as a string of hexadecimal digits. Overall, System.Security.Cryptography HMACSHA256 is a powerful and flexible package library in csharp that can be used to ensure the integrity and authenticity of messages by generating secure message authentication codes.