gpt4 book ai didi

c++ - 在 C++ 中实现 SHA-256

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:21:53 25 4
gpt4 key购买 nike

我正在尝试在 MSVC++ 中实现 SHA-256。除了将消息计划数组的前 16 个单词扩展到剩余的 48 个单词之外,我快到了。我已经确定我的问题在这一点上,因为它与 example from nist.gov 完全匹配。直到压缩算法的第 17 轮。我的消息时间表代码如下:

//Extend first 16 words into the remaining 48 words of the message schedule array:
for (int k = 16; k < 64; k++)
{
bitset<32> s0 = rotr(W[k - 15], 7) ^= rotr(W[k - 15], 18) ^= (W[k - 15] >> 3);
bitset<32> s1 = rotr(W[k - 2], 17) ^= rotr(W[k - 2], 19) ^= (W[k - 2] >> 10);
W[k] = add(add(W[k - 16], s0), add(W[i - 7], s1));
}

bitset<32> add(bitset<32> a, bitset<32> b)
{
unsigned long c = a.to_ulong();
unsigned long d = b.to_ulong();
return bitset<32>((c + d) % 4294967296);
}

bitset<32> rotr(bitset<32> b, int num)
{
int temp = (int)b.to_ulong();
temp = _rotr(temp, num);
return bitset<32> (temp);
}

其中 W[0..15] 是填充消息的拷贝(与示例匹配)。有人看到问题了吗?完整代码是 here.大约有 170 行。

最佳答案

这个问题已经很老了,但还没有被回答或关闭,所以我认为仍然欢迎回答

在将消息计划数组的前 16 个字扩展到剩余的 48 个字时,您可能意味着第三次调用 add 函数以读取 add(W[k-7] , s1) 而不是 add(W[i-7], s1)

前 7 个 block 的数组索引 i-7 小于零。

关于c++ - 在 C++ 中实现 SHA-256,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29313571/

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