Skip to main content

Computes a Hash-based Message Authentication Code (HMAC) by using the SHA256 hash function.

namespace Util
{
    using System;
    using System.Security.Cryptography;
    using System.Text;

    internal static class CryptoUtility
    {
        internal static string ComputeHmac256(byte[] key, string message)
        {
            using (HashAlgorithm hashAlgorithm = new HMACSHA256(key))
            {
                byte[] messageBuffer = Encoding.UTF8.GetBytes(message);
                return Convert.ToBase64String(hashAlgorithm.ComputeHash(messageBuffer));
            }
        }
    }
}