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 12:43:08 pm
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(1)
 [1] Santiago | 2009-09-17 15: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
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[116399]
2D Rotated Rectangles Collision Detection[88469]
Keyboard Hook[76725]
UDP[65659]
HTTP Proxy[41049]

::Top 5 Samples::
2D ColDet Rotated Rectangles[11531]
PS2 Mouse Driver[6928]
Wave Format Player[5764]
Reading FAT12[5591]
CodeGuru[5327]


All rights reserved to RageStorm © 2009