如何计算.NET 4.5 Core中的HMAC-SHA1身份验证代码 [英] How to calculate HMAC-SHA1 authentication code in .NET 4.5 Core

查看:686
本文介绍了如何计算.NET 4.5 Core中的HMAC-SHA1身份验证代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前面临一个大问题(环境:.NET 4.5 Core):我们需要使用HMAC-SHA1算法使用密钥保护消息。问题是命名空间 System.Security.Cryptography 的HMACSHA1类和命名空间本身不存在于.NET 4.5 Core中,此命名空间只存在于正常版本的.NET。



我尝试了很多方法来为我们的目的找到一个等效的命名空间,但我发现的唯一的东西是 Windows.Security。密码学这可悲地不提供HMAC加密。



有没有人知道我该如何解决我们的问题,或有任何自由使用第三方解决方案?

c> c> c>



blockquote>

通过调用静态的 OpenAlgorithm 方法创建一个 MacAlgorithmProvider 以下算法
names: 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

  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();

} b $ b


I’m currently facing a big problem (Environment: .NET 4.5 Core): We need to protect a message with a key using a HMAC-SHA1 algorithm. The problem is that the HMACSHA1-class of the namespace System.Security.Cryptography and the namespace itself do not exist in .NET 4.5 Core, this namespace only exists in the normal version of .NET.

I tried a lot of ways to find an equivalent namespace for our purpose but the only thing I found was Windows.Security.Cryptography which sadly does not offer a HMAC-Encryption.

Does anyone have an idea how I could solve our problem or is there any free to use 3rd-party solution?

解决方案

The Windows.Security.Cryptography namespace does contain HMAC.

You create a MacAlgorithmProvider object by calling the static OpenAlgorithm method and specifying one of the following algorithm names: 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

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();

}

这篇关于如何计算.NET 4.5 Core中的HMAC-SHA1身份验证代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆