gpt4 book ai didi

c# - 是否有符合 .NET FIPS 的键控 SHA256 哈希算法?

转载 作者:行者123 更新时间:2023-11-30 14:32:48 29 4
gpt4 key购买 nike

我正在使用 HMACSHA256 和以下代码创建键控 SHA256 哈希:

HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secretKey);
byte[] hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(data));

string hashResult = string.Empty;
for (int i = 0; i < hash.Length; i++)
{
hashResult += hash[i].ToString("x2"); // hex format
}

这工作得很好,但是,它在启用 FIPS 的环境中失败,因为 HMACSHA256 使用底层 SHA256Managed 实现,它本身不符合 FIPS。

通过 MSDN 文档搜索,我发现 KeyedHashAlgorithm 的唯一 SHA256 实现是 HMACSHA256。

我需要使用键控 SHA256 哈希对 Web 服务请求进行签名(因此我无法更改哈希类型),并且我必须能够在启用 FIPS 的环境中运行。

Google 显示 SHA256CryptoServiceProvider 和 SHA256Cng 都是创建 SHA256 哈希的 FIPS 兼容方法,但似乎都不支持创建键控哈希。

最佳答案

我知道这已经过时了,但看起来 Microsoft 解决了这个问题。我在 Windows 8 上运行 .NET 4.5.1。我不知道这是修复的 BCL 版本还是操作系统。

this.m_hash1 = HMAC.GetHashAlgorithmWithFipsFallback((Func<HashAlgorithm>) (() => (HashAlgorithm) new SHA256Managed()), (Func<HashAlgorithm>) (() => HashAlgorithm.Create("System.Security.Cryptography.SHA256CryptoServiceProvider")));

关于c# - 是否有符合 .NET FIPS 的键控 SHA256 哈希算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17862491/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com