HMACSHA1 off by 2 bytes
I am using this class in MSVC++ 2010 Express: http://www.codeproject.com/KB/recipes/HMACSHA1class.aspx. I am running Vista 32bit. Pretty much to get it working I just changed...
SHA1.cpp:
fIn = fopen(szFileName, "rb");
to
fIn = fopen_s(szFileName, "rb");
because without this change it would say: "error C3861: 'fopen': identifier not found".
the code I used in int main is:
BYTE Key[20] ;
BYTE digest[20] ;
unsigned char test[] = "Hi There" ;
mems开发者_StackOverflowet(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
The problem is my digest is equal to: 0xb61731865557264e28bc0b6fb378c8ef146be0 and it suppose to be equal to 0xb617318655057264e28bc0b6fb378c8ef146be00. Any help on what is wrong with this code and how to get it working would help a lot...
Or maybe someone can point me into the right direction for a better HMACSHA1 class. CryptoAPI for win32 is to complex and silly.
I guess it's because some byte-values in digest are < 10 which means only one character is written.
Maybe this solves your problem:
for(int i=0;i<sizeof(digest);i++)
std::cout << setfill('0') << setw(2) << hex << (int)digest[i];
精彩评论