netcore 下加密遇到的问题

   KeyedHashAlgorithm algorithm = KeyedHashAlgorithm.Create(algorithmName.ToString().ToUpper(CultureInfo.InvariantCulture));
                if (null == algorithm)
                    throw new InvalidOperationException("Please specify a KeyedHashAlgorithm to use.");

                try
                {
                    algorithm.Key = key;
                    byte[] bytes = algorithm.ComputeHash(data);
                    return bytes;
                }
                finally
                {
                    algorithm.Clear();
                }

这个代码会报异常,出现PNSE

经查找各种文档发现一下使用办法是Ok的,有没有其它的办法呢?

  try
                {
                    var keyBytes = Encoding.UTF8.GetBytes(key);
                    var hmac = new HMACSHA1(keyBytes);
                    byte[] bytes = hmac.ComputeHash(data);
                    return Convert.ToBase64String(bytes);
                }
                catch (Exception e)
                {
                    throw e;
                }
                finally
                {
                }

HMACSHA256 hashAlgorithm = new HMACSHA256(key); return hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(data));
原文地址:https://www.cnblogs.com/xyfy/p/7845235.html