C++ HMACSHA1关闭2个字节

C++ HMACSHA1关闭2个字节,c++,cryptography,sha1,hmac,hmacsha1,C++,Cryptography,Sha1,Hmac,Hmacsha1,我正在MSVC++2010 Express中使用此类:。我正在运行Vista 32位。为了让它正常工作,我刚换了 SHA1.cpp: fIn = fopen(szFileName, "rb"); 到 因为如果没有此更改,它将显示:“错误C3861:‘fopen’:找不到标识符” 我在int main中使用的代码是: BYTE Key[20] ; BYTE digest[20] ; unsigned char test[] = "Hi There" ; memset(Key, 0x0b, 20

我正在MSVC++2010 Express中使用此类:。我正在运行Vista 32位。为了让它正常工作,我刚换了

SHA1.cpp:

fIn = fopen(szFileName, "rb");

因为如果没有此更改,它将显示:“错误C3861:‘fopen’:找不到标识符”

我在int main中使用的代码是:

BYTE Key[20] ;
BYTE digest[20] ; 
unsigned char test[] = "Hi There" ;
memset(Key, 0x0b, 20) ;
CHMAC_SHA1 HMAC_SHA1 ;
HMAC_SHA1.HMAC_SHA1(test, strlen((const char *)test), Key, sizeof(Key), digest) ;

for(int i=0;i<sizeof(digest);i++)
    std::cout << hex << (int)digest[i];

int a;
std::cin >> a;

// Check with digest equal to 0xb617318655057264e28bc0b6fb378c8ef146be00
// or not
字节键[20];
字节摘要[20];
无符号字符测试[]=“你好”;
memset(键,0x0b,20);
CHMAC_SHA1 HMAC_SHA1;
HMAC_SHA1.HMAC_SHA1(测试,strlen((const char*)测试),键,大小(键),摘要);

对于(inti=0;i我想这是因为摘要中的某些字节值小于10,这意味着只写入了一个字符

也许这能解决你的问题:

for(int i=0;i<sizeof(digest);i++)
    std::cout << setfill('0') << setw(2) << hex << (int)digest[i];

for(inti=0;i我想这是因为摘要中的某些字节值小于10,这意味着只写入了一个字符

也许这能解决你的问题:

for(int i=0;i<sizeof(digest);i++)
    std::cout << setfill('0') << setw(2) << hex << (int)digest[i];

用于It i=0;实际上这不是一个实际哈希创建的问题,而是用您的输出代码。我不熟悉C++流API,但是因为它省略了代码> 0 < /Cule> s,也许它将每个字节作为一个或两个char十六进制字符串输出,省略了领先的代码> 0 <代码>。这与实际的哈希CREA看起来不一样。但是,我不熟悉C++流API,但是因为它省略了代码> 0 < /代码> s,也许它将每个字节输出为一个或两个字符的十六进制字符串,省略了前面的代码> 0 < /代码> s。