Skip to content

Instantly share code, notes, and snippets.

@mobius
Created August 17, 2016 08:50
Show Gist options
  • Save mobius/a2f9847bbce474f71d6478948ef34d25 to your computer and use it in GitHub Desktop.
Save mobius/a2f9847bbce474f71d6478948ef34d25 to your computer and use it in GitHub Desktop.
bool PatchManager::GetMD5(const u8 *pBuffer, u32 uBufLen, u8 *rgbHash)
{
BOOL bResult = FALSE;
HCRYPTPROV hProv = 0;
HCRYPTHASH hHash = 0;
DWORD cbRead = 0;
DWORD cbHash = 0;
CHAR rgbDigits[] = "0123456789ABCDEF";
// Get handle to the crypto provider
if (!CryptAcquireContext(&hProv,
NULL,
NULL,
PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT))
{
return false;
}
if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
{
CryptReleaseContext(hProv, 0);
return false;
}
u32 uIdx = 0;
while (uIdx < uBufLen)
{
u32 cbRead = 1024;
if (uIdx + cbRead > uBufLen)
{
cbRead = uBufLen - uIdx;
}
if (!CryptHashData(hHash, pBuffer + uIdx, cbRead, 0))
{
CryptReleaseContext(hProv, 0);
CryptDestroyHash(hHash);
return false;
}
uIdx += cbRead;
}
cbHash = 16;
if (CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0))
{
bResult = TRUE;
}
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
return bResult;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment