在C++中实现SHA-256
Implementing SHA-256 in C++
我正在尝试在MSVC++中实现SHA-256。除了将消息调度数组的前16个字扩展到剩下的48个字之外,我几乎做到了。我已经确定我的问题是在这一点上,因为它与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]是填充消息的副本(与示例匹配)。有人看到问题了吗?完整的代码在这里。大约有170行。
这个问题已经很老了,但它还没有得到回答或关闭,所以我认为答案仍然是受欢迎的
将消息调度数组的前16个字扩展到剩下的48个字中,您可能是想让对add
函数的第三次调用读取add(W[k-7], s1)
而不是add(W[i-7], s1)
对于前7个块,数组索引i-7
小于零。
相关文章:
- 如果没有malloc,链表实现将失败
- 如何在c++中实现处理器调度模拟器
- 如何在c++中使用引用实现类似python的行为
- 实现无开销push_back的最佳方法是什么
- 使用简单类型列表实现的指数编译时间.为什么
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 如何在BST的这个简单递归实现中消除警告
- 实现一个在集合上迭代的模板函数
- 我应该实现右值推送功能吗?我应该使用std::move吗
- std::当在256字节边界上写入整数时,流的奇怪行为
- 如何正确实现和访问运算符的各种自定义枚举器
- C++Union/Struct位域的实现和可移植性
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 在c++中实现LinkedList时,应出现未处理的错误
- 为左值和右值的包装器实现C++范围
- SHA-256的双分部分的十六进制表示
- 创建密码的 SHA-256 哈希的 Botan 教程或示例
- 在C++中实现SHA-256
- c++中计算sha-256的一个很好的库
- SHA-1的c++实现中的错误散列