Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.2 KiB

  1. #pragma once
  2. class MD5Hash
  3. {
  4. public:
  5. MD5Hash() {ZeroMemory(dwHash, sizeof(dwHash));}
  6. BSTR GetHashBSTR()
  7. {
  8. TCHAR szHash[33];
  9. for(int i=0;i<4;i++)
  10. StringCchPrintf(&szHash[i*8],sizeof(szHash)-(i * 8),_T("%08X"),dwHash[i]);
  11. CComBSTR bstr(szHash);
  12. return bstr.Detach();
  13. }
  14. HRESULT HashData(LPBYTE pData, DWORD dwSize)
  15. {
  16. HRESULT hr = E_FAIL;
  17. HCRYPTPROV hProv = NULL;
  18. HCRYPTHASH hHash = NULL;
  19. __try
  20. {
  21. if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
  22. __leave;
  23. if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
  24. __leave;
  25. if(!CryptHashData(hHash, pData, dwSize, 0))
  26. __leave;
  27. DWORD dwSizeHash = 0;
  28. DWORD dwSizeDWORD = sizeof(dwSizeHash);
  29. if(!CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE*)&dwSizeHash, &dwSizeDWORD, 0))
  30. __leave;
  31. if(dwSizeDWORD != sizeof(dwSizeHash) || dwSizeHash != sizeof(dwHash))
  32. __leave;
  33. if(!CryptGetHashParam(hHash, HP_HASHVAL, (BYTE*)dwHash, &dwSizeHash, 0))
  34. __leave;
  35. hr = S_OK;
  36. }
  37. __finally
  38. {
  39. if(hHash)
  40. CryptDestroyHash(hHash);
  41. if(hProv)
  42. CryptReleaseContext(hProv, 0);
  43. }
  44. return hr;
  45. }
  46. protected:
  47. DWORD dwHash[4];
  48. };