Home
Tutorials
Code Snippets
Code Samples
Downloads
Links

The Blog
Our Projects
About
Contact

::Add RageStorm to Favorites!::

The Blog | Our Projects | Guest Book | About | Contact

 
Code Snippet - Generate MD5 Checksum
Author:Arkon
Category:Win32API
File Size:951 Bytes
Uploaded at:20-Nov-06 07:43:08 am
Description:
  Using the CryptoAPI to generate an MD5 checksum for any data.
  
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]);
}
User Contributed Comments(2)
 [1] Santiago | 2009-09-17 11:57:44

Great .. saved a lot of time. I needed one and could'nt find right examples at MSDN. yours was cut and paste for me. Thanks Again
 [2] Kostya | 2013-01-23 06:30:23

You must use CryptDestroyHash() to free the hash object after you are finished using it.
NOTE:
Comments that will hurt anyone in any way will be deleted.
Don't ask for features, advertise or curse.
If you want to leave a message to the author use the contacts,
if you have any question in relation to your comments please use the forum.
Comments which violate any of these requests will be deleted without further
notice. Use the comment system decently.

Post your comment:
Name:
email:
Comment:
::Top 5 Tutorials::
Embedded Python[105418]
2D Rotated Rectangles Collision Detection[78723]
Keyboard Hook[68414]
UDP[52506]
HTTP Proxy[35823]

::Top 5 Samples::
2D ColDet Rotated Rectangles[10562]
PS2 Mouse Driver[6063]
Wave Format Player[4945]
Reading FAT12[4828]
CodeGuru[4521]


All rights reserved to RageStorm © 2009