我的代码中的HMAC-SHA512错误
HMAC-SHA512 bug in my code
我将非常感激,如果你能帮助我这个c++实现的HMAC-SHA512代码,我似乎找不到为什么它给出了一个不同的哈希比在线转换器。(SHA512工作正常)
代码(基于维基百科):
#include <iostream>
#include "sha512.h"
using namespace std;
const unsigned int BLOCKSIZE = (512/8); // 64 byte
int main(int argc, char *argv[])
{
if(argc!=3)return 0;
string key = argv[1];
string message = argv[2];
if(key.length() > BLOCKSIZE){
key = sha512(key);
}
while(key.length() < BLOCKSIZE){
key = key + (char)0x00;
}
string o_key_pad = key;
for(unsigned int i = 0; i < BLOCKSIZE; i++){
o_key_pad[i] = key[i] ^ (char)0x5c;
}
string i_key_pad = key;
for(unsigned int i = 0; i < BLOCKSIZE; i++){
i_key_pad[i] = key[i] ^ (char)0x36;
}
string output = sha512(o_key_pad + sha512(i_key_pad + message));
cout<<"hmac-sha512: n"<<output<<endl;
return 0;
}
原来BLOCKSIZE是错误的
根据http://en.wikipedia.org/wiki/SHA-2, sha-512的块大小是1024位,也就是128字节。
所以只需将代码改为
const unsigned int BLOCKSIZE = (1024/8); // 128 byte
你得到了正确的结果
感谢您的快速响应,问题出在哈希函数上(某种程度上)。sha512输出在返回之前被转换为十六进制,因此"sha512(i_key_pad +消息)"没有回答我所期望的。(块大小也是1024)
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- QCA-OSSL 插件未加载.显示错误:不支持 HMAC(SHA1)
- 我的代码中的HMAC-SHA512错误