public static string HmacWithKey(string key, byte[] data) { byte[] keyByte = Encoding.UTF8.GetBytes(key); #if WINDOWS_UWP || WINDOWS_WSA var hmacsha256 = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256); #else using (var hmacsha256 = new HMACSHA256(keyByte)) #endif { #if WINDOWS_UWP || WINDOWS_WSA var input = data.AsBuffer(); var signatureKey = hmacsha256.CreateKey(keyByte.AsBuffer()); var cypherMac = CryptographicEngine.Sign(signatureKey, input); return(CryptographicBuffer.EncodeToBase64String(cypherMac)); #else byte[] hashmessage = hmacsha256.ComputeHash(data); return(Convert.ToBase64String(hashmessage)); #endif } }