如何在Windows上计算HMAC SHA?
我需要在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
CryptImportKey
将密钥获取到 Windows 加密服务提供程序中。然后按照 MSDN 示例 HMAC 操作 代码。将密钥放入 CSP 的技巧是创建一个结构来保存 3 个内容:BLOBHEADER
、用于长度的DWORD
以及用于密钥的 char[]。我假设您拥有原始密钥数据,因此它看起来像这样:确保将长度替换为密钥的实际长度。
You can use
CryptImportKey
to 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: aBLOBHEADER
, aDWORD
for the length, and char[] for the key. I will presume you have the raw key data so it would look something like:Make sure to replace the lengths with the actual length of your key.
如果您显式拥有密钥,则通过 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.
如果您想使用操作系统中包含的 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