【问题标题】:How to calculate HMAC-SHA1 authentication code in .NET 4.5 Core如何在 .NET 4.5 Core 中计算 HMAC-SHA1 身份验证码
【发布时间】:2013-01-11 13:37:47
【问题描述】:

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

我尝试了很多方法来为我们的目的找到一个等效的命名空间,但我唯一找到的是Windows.Security.Cryptography,遗憾的是它不提供 HMAC 加密。

有没有人知道我可以如何解决我们的问题,或者是否有任何免费使用的 3rd-party 解决方案?

【问题讨论】:

  • 澄清一下,当您提到 .net 4.5 核心时,您指的是 .net 4.5 的 win8 api 子集,这就是您无权访问 System.Security.Cryptography 的原因吗?跨度>

标签: c# .net cryptography hmac hmacsha1


【解决方案1】:

Windows.Security.Cryptography 命名空间确实包含 HMAC。

您通过调用静态OpenAlgorithm 方法并指定以下算法之一来创建MacAlgorithmProvider 对象 姓名: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();

}

【讨论】:

  • 感谢您的快速回复。这很有帮助,我能够成功运行 CreateHMAC 方法(在 technet msdn.microsoft.com/en-us/library/windows/apps/xaml/… 上描述),但我需要的是这样的:HMAC_SHA1("key", "The quick brown fox jumps over the lazy dog") = 0xde7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9 Do你知道我如何检索这样的哈希值。提前谢谢你
猜你喜欢
  • 1970-01-01
  • 2018-02-08
  • 1970-01-01
  • 1970-01-01
  • 2018-12-17
  • 2020-10-28
  • 2020-10-15
  • 2018-05-28
  • 1970-01-01
相关资源
最近更新 更多