首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >什么是双HMAC验证以及它是如何工作的?

问什么是双HMAC验证以及它是如何工作的?
EN

Stack Overflow用户
提问于 2022-04-30 14:26:11
回答 1查看 167关注 0票数 1

来自超级安全白皮书

API不存储用户发送的未散列身份验证密钥。它只存储哈希身份验证密钥,以防止“传递-哈希”攻击(在泄露数据库的情况下,攻击者只需传递哈希身份验证密钥以获得身份验证并以实际用户身份执行操作)。服务器总是对从客户端接收的身份验证密钥进行散列,从而防止此攻击向量。如果哈希身份验证密钥与数据库中的结果不匹配,则API将以否定的响应响应指示失败。API端通过使用双HMAC验证(https://www.nccgroup.trust/us/about-us/newsroom-and-events/blog/2011/february/double-hmac-verification/)来避免定时攻击。

不幸的是,链接已经失效,谷歌搜索经常指向同一个来源。当您需要原始密钥来验证签名时,请解释一下双hmac验证是如何工作的吗?谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-01 13:17:30

该URL已被Wayback机器存档,所以您仍然可以阅读完整的博客文章:http://web.archive.org/web/20160203044316/https://www.nccgroup.trust/us/about-us/newsroom-and-events/blog/2011/february/double-hmac-verification/

post包含一个C#代码片段,演示如何执行双HMAC验证。最后非常简单,正如名称"double HMAC“表示它使用相同的HMAC键执行两次HMAC一样。

代码语言:javascript
复制
public void validateHMACSHA256(byte[]receivedHMAC, byte[]message, byte[]key) {
    HashAlgorithm hashAlgorithm = new HMACSHA256(key);

    // size and algorithm choice are not secret; no weakness in failing fast here.
    if (receivedHMAC.Length != hashAlgorithm.HashSize / 8) {
        Throw new CryptographicException("HMAC verification failure.");
    }
    byte[]calculatedHMAC = hashAlgorithm.ComputeHash(message);
    // Now we HMAC both values again before comparing to randomize byte order.
    // These two lines are all that is required to prevent many existing implementations
    // vulnerable to adaptive chosen ciphertext attacks using the timing side channel.
    receivedHMAC = hashAlgorithm.ComputeHash(receivedHMAC);
    calculatedHMAC = hashAlgorithm.ComputeHash(calculatedHMAC);

    for (int i = 0; i < calculatedHMAC.Length; i++) {
        if (receivedHMAC[i] != calculatedHMAC[i]) {
            throw new CryptographicException("HMAC verification failure.");
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72069351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档