bool MD5(const unsigned char* input, unsigned int inputLen, unsigned char md5Hash[16]) { HCRYPTHASH hCryptHash; HCRYPTPROV hCryptProv = NULL;
ZeroMemory(md5Hash, sizeof(md5Hash));
if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) return false;
// Select MD5 algorithm. if (!CryptCreateHash(hCryptProv, CALG_MD5, NULL, 0, &hCryptHash)) { CryptReleaseContext(hCryptProv, 0); return false; }
// Really hash the data. CryptHashData(hCryptHash, input, inputLen, 0); DWORD md5HashLen = 16; // Store result into given buffer. CryptGetHashParam(hCryptHash, HP_HASHVAL, md5Hash, &md5HashLen, 0);
CryptReleaseContext(hCryptProv,0); return true; }
// Usage example: unsigned char md5Result[16] = {0}; char data[] = "hash my data"; MD5((const unsigned char*)data, sizeof(data) - 1, md5Result); for (int i = 0; i < sizeof(md5Result); i++) { printf("%02x", md5Result[i]); }
|