MD5 - different results
-
Hy, I have my next problem. I am trying to get an md5 hash from an string (i am using "hello" in this example) The problem is, that the CryptoAPI gives me a different key than the md5 function from PHP. I think MD5 is MD5 so both should give me the same result. Here is my Code for the crypto api -> error() only shows Error Nr.: id as a small help for me. The ByteToStr function is the same ByteToStr function which is in the MSDN.
HCRYPTPROV hProv = NULL; HCRYPTHASH hHash = NULL; BYTE \*pbKey = (BYTE\*)"hello"; DWORD dwKeyLen = strlen((char\*)pbKey)+1; DWORD dwHashLen = 0; BYTE pbOut\[16\]; if(!CryptAcquireContext(&hProv,NULL,NULL,PROV\_RSA\_FULL,0)) Error(1); if(!CryptCreateHash(hProv,CALG\_MD5,0,0,&hHash)) Error(2); if(!CryptHashData(hHash,pbKey,dwKeyLen,0)) Error(3); if(!CryptGetHashParam(hHash,HP\_HASHVAL,NULL,&dwHashLen,0)) Error(4); if(!CryptGetHashParam(hHash,HP\_HASHVAL,pbOut,&dwHashLen,0)) Error(5); LPCSTR lpMsg = new char\[100\]; ByteToStr(dwHashLen,&pbOut,(LPSTR)lpMsg); MessageBoxA(NULL,lpMsg,"hihi",0); delete lpMsg; CryptDestroyHash(hHash); CryptReleaseContext(hProv,0);
Thanks for help. bye, gabbana
-
Hy, I have my next problem. I am trying to get an md5 hash from an string (i am using "hello" in this example) The problem is, that the CryptoAPI gives me a different key than the md5 function from PHP. I think MD5 is MD5 so both should give me the same result. Here is my Code for the crypto api -> error() only shows Error Nr.: id as a small help for me. The ByteToStr function is the same ByteToStr function which is in the MSDN.
HCRYPTPROV hProv = NULL; HCRYPTHASH hHash = NULL; BYTE \*pbKey = (BYTE\*)"hello"; DWORD dwKeyLen = strlen((char\*)pbKey)+1; DWORD dwHashLen = 0; BYTE pbOut\[16\]; if(!CryptAcquireContext(&hProv,NULL,NULL,PROV\_RSA\_FULL,0)) Error(1); if(!CryptCreateHash(hProv,CALG\_MD5,0,0,&hHash)) Error(2); if(!CryptHashData(hHash,pbKey,dwKeyLen,0)) Error(3); if(!CryptGetHashParam(hHash,HP\_HASHVAL,NULL,&dwHashLen,0)) Error(4); if(!CryptGetHashParam(hHash,HP\_HASHVAL,pbOut,&dwHashLen,0)) Error(5); LPCSTR lpMsg = new char\[100\]; ByteToStr(dwHashLen,&pbOut,(LPSTR)lpMsg); MessageBoxA(NULL,lpMsg,"hihi",0); delete lpMsg; CryptDestroyHash(hHash); CryptReleaseContext(hProv,0);
Thanks for help. bye, gabbana
gabbana wrote:
DWORD dwKeyLen = strlen((char*)pbKey)+1;
Ummmm - is the PHP MD5 algorithm including a NULL terminator byte? Do PHP strings even have NULL terminators? I doubt it very much... If removing the NULL terminator doesn't fix it, one other thing to consider is whether or not PHP strings are ASCII or some form of multi-byte Unicode?
-
gabbana wrote:
DWORD dwKeyLen = strlen((char*)pbKey)+1;
Ummmm - is the PHP MD5 algorithm including a NULL terminator byte? Do PHP strings even have NULL terminators? I doubt it very much... If removing the NULL terminator doesn't fix it, one other thing to consider is whether or not PHP strings are ASCII or some form of multi-byte Unicode?