我目前面临一个大问题(环境:.NET 4.5 Core):我们需要使用 HMAC-SHA1 算法通过密钥来保护消息。问题是命名空间的 HMACSHA1 类System.Security.Cryptography
并且命名空间本身并不存在于.NET 4.5 Core中,该命名空间仅存在于普通版本的.NET中。
我尝试了很多方法来找到符合我们目的的等效名称空间,但我发现的唯一的就是Windows.Security.Cryptography
遗憾的是它不提供 HMAC 加密。
有谁知道我如何解决我们的问题或者是否有任何免费使用的第三方解决方案?
The Windows.Security.Cryptography
命名空间确实包含 HMAC。
你创建一个MacAlgorithmProvider
通过调用静态对象OpenAlgorithm
方法并指定以下算法之一
名称:HMAC_MD5 HMAC_SHA1 HMAC_SHA256 HMAC_SHA384 HMAC_SHA512 AES_CMAC
http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.core.macalgorithmprovider.aspx http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.core.macalgorithmprovider.aspx
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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)