Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[c++ snippet] MD5 of string using wincrpyt API

Featured Replies

Posted

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 :)

Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.