deepzero Posted February 4, 2011 Posted February 4, 2011 Hi, I always thought calculating the MD5 digest of a string required adding external code, i was using this: http://www.zedwood.com/article/121/cpp-md5-function which works fine. I was messing around with Windows crypt api, and noticed one can easily calculate the digest using WinCrypt APIs: string MD5(string input){ HCRYPTPROV CryptProv; HCRYPTHASH CryptHash; BYTE BytesHash[33];//! DWORD dwHashLen; string final; if(CryptAcquireContext(&CryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) { if(CryptCreateHash(CryptProv, CALG_MD5, 0, 0, &CryptHash)) { if(CryptHashData(CryptHash, (BYTE*)input.c_str(), input.length(), 0)) { if(CryptGetHashParam(CryptHash, HP_HASHVAL, BytesHash, &dwHashLen, 0)) { final.clear(); string hexcharset = "0123456789ABCDEF"; for(int j = 0; j < 16; j++) { final += hexcharset.substr(((BytesHash[j] >> 4) & 0xF),1); final += hexcharset.substr(((BytesHash[j]) & 0x0F),1); } } } } } CryptDestroyHash(CryptHash); CryptReleaseContext(CryptProv, 0); return final; } (dont forget #include <wincrypt.h>) can be tweaked for other hashes, too. Just check the definitions in <wincrypt.h>. nice weekend, everyone dp0
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now