如何在Windows上计算HMAC SHA?

发布于 2024-11-28 09:40:23 字数 350 浏览 0 评论 0原文

我需要在 Windows 上的程序中计算 HMAC SHA。该程序之前曾在 Linux 上运行,其中使用了 openssl。现在我需要将其移植到Windows,但我不确定Windows平台SDK是否提供任何计算HMAC SHA的方法。

我在 msdn 上发现了以下链接,但我不确定 - http://msdn.microsoft.com/en-us/library/aa382453(v=VS.85).aspx

让我知道对我来说最好的未来是什么。现有程序是C语言的。

I need to calculate HMAC SHA in my program on Windows. This program earlier used to run on linux where it used the openssl. Now I need to port it to Windows, but I am not sure if Windows platform SDK provides any means to calculate the HMAC SHA.

I cam across the following link on msdn, but I am not sure - http://msdn.microsoft.com/en-us/library/aa382453(v=VS.85).aspx.

Let me know what is the best way ahead for me. The existing program is in C.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只为一人 2024-12-05 09:40:23

您可以使用CryptImportKey将密钥获取到 Windows 加密服务提供程序中。然后按照 MSDN 示例 HMAC 操作 代码。将密钥放入 CSP 的技巧是创建一个结构来保存 3 个内容:BLOBHEADER、用于长度的 DWORD 以及用于密钥的 char[]。我假设您拥有原始密钥数据,因此它看起来像这样:

struct KeyData
{
  BLOBHEADER hdr;
  unsigned long keyLength;
  unsigned char key[128];
};

void ComputeHash()
{
  HCRYPTPROV cryptoProvider = 0;
  HCRYPTKEY cryptKey = 0;
  KeyData kd;
  kd.hdr.aiKeyAlg = CALG_RC2;
  kd.hdr.bType = PLAINTEXTKEYBLOB;
  kd.hdr.bVersion = CUR_BLOB_VERSION;
  kd.hdr.reserved = 0;
  kd.keyLength = 128;
  /* set your key data here */

  /* get a crypto provider - See the microsoft references 
     This example selects "Microsoft AES Cryptographic Provider"
     which supports SHA-256
  */
  CryptAcquireContext(&cryptoProvider, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_VERIFYCONTEXT);

  CryptImportKey(cryptoProvider, reinterpret_cast<BYTE*>(&kd), sizeof(kd),0, CRYPT_IPSEC_HMAC_KEY, &cryptKey);
  /* use cryptKey in the call to CryptCreateHash when you create your CALG_HMAC */
}

确保将长度替换为密钥的实际长度。

You can use CryptImportKeyto get your key into the Windows Cryptographic Service Provider. Then follow the MSDN example HMAC code. The trick to getting your key into the CSP is to make a struct to hold 3 things: a BLOBHEADER, a DWORD for the length, and char[] for the key. I will presume you have the raw key data so it would look something like:

struct KeyData
{
  BLOBHEADER hdr;
  unsigned long keyLength;
  unsigned char key[128];
};

void ComputeHash()
{
  HCRYPTPROV cryptoProvider = 0;
  HCRYPTKEY cryptKey = 0;
  KeyData kd;
  kd.hdr.aiKeyAlg = CALG_RC2;
  kd.hdr.bType = PLAINTEXTKEYBLOB;
  kd.hdr.bVersion = CUR_BLOB_VERSION;
  kd.hdr.reserved = 0;
  kd.keyLength = 128;
  /* set your key data here */

  /* get a crypto provider - See the microsoft references 
     This example selects "Microsoft AES Cryptographic Provider"
     which supports SHA-256
  */
  CryptAcquireContext(&cryptoProvider, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_VERIFYCONTEXT);

  CryptImportKey(cryptoProvider, reinterpret_cast<BYTE*>(&kd), sizeof(kd),0, CRYPT_IPSEC_HMAC_KEY, &cryptKey);
  /* use cryptKey in the call to CryptCreateHash when you create your CALG_HMAC */
}

Make sure to replace the lengths with the actual length of your key.

风吹短裙飘 2024-12-05 09:40:23

如果您显式拥有密钥,则通过 SHA 原语的两次调用显式地对 HMAC 进行编程可能比尝试让 Windows 加密 API 知道该密钥更容易。 HMAC 本身在 RFC 2104 中指定,Windows 可以使用 CryptCreateHash、CryptHashData 和 CryptGetHashParam 为您执行 SHA 哈希。

If you have the key explicitly, it may be easier to program HMAC explicitly out of two invocations of the SHA primitive than to try to get make the key known to the Windows crypto API. HMAC itself is specified in RFC 2104, and Windows can do the SHA hashes for you with CryptCreateHash, CryptHashData and CryptGetHashParam.

北城半夏 2024-12-05 09:40:23

如果您想使用操作系统中包含的 API,那么您找到的链接就可以 - 更多信息请参阅 http://msdn.microsoft.com/en-us/library/aa380255%28v=vs.85%29.aspx

或者您是否正在寻找具有某些特定功能的第三方库特征 ?如果您已经熟悉 openssl,它也适用于 Windows...请参阅 http:// www.slproweb.com/products/Win32OpenSSL.html

IF you want to use the API included in the OS then the link you found is ok - more information see http://msdn.microsoft.com/en-us/library/aa380255%28v=vs.85%29.aspx

Or are you looking for some 3rd-party lib with some specific features ? if you are already familiar with with openssl, it is available for Windows too... see http://www.slproweb.com/products/Win32OpenSSL.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文