如何在.net 4.5 Core中计算HMAC-SHA1认证码
本文关键字:计算 HMAC-SHA1 认证 Core net | 更新日期: 2023-09-27 17:52:15
我目前面临一个大问题(环境:. net 4.5 Core):我们需要使用HMAC-SHA1算法保护带有密钥的消息。问题是命名空间System.Security.Cryptography
的hmacsha1类和命名空间本身在。net 4.5 Core中不存在,这个命名空间只存在于。net的正常版本中。
我尝试了很多方法来为我们的目的找到一个等效的命名空间,但我唯一发现的是Windows.Security.Cryptography
,遗憾的是它不提供hmac加密。
有没有人知道如何解决我们的问题,或者有任何免费使用的第三方解决方案?
Windows.Security.Cryptography
命名空间包含HMAC。
http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.core.macalgorithmprovider.aspx通过调用静态
OpenAlgorithm
方法并指定以下算法之一来创建MacAlgorithmProvider
对象名称:HMAC_MD5 HMAC_SHA1 HMAC_SHA256 HMAC_SHA384 HMAC_SHA512 AES_CMAC
public static byte[] HmacSha1Sign(byte[] keyBytes, string message){
var messageBytes= Encoding.UTF8.GetBytes(message);
MacAlgorithmProvider objMacProv = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
CryptographicKey hmacKey = objMacProv.CreateKey(keyBytes.AsBuffer());
IBuffer buffHMAC = CryptographicEngine.Sign(hmacKey, messageBytes.AsBuffer());
return buffHMAC.ToArray();
}