#include <wincrypt.h>
DWORD Gethash (CONST BYTE*pbdata, DWORD Dwdatalen, alg_id algid, LPTSTR Pszhash) {DWORD Dwreturn=0; Hcryptprov Hprov; if( ! CryptAcquireContext (&Hprov, NULL, NULL, Prov_rsa_full, Crypt_verifycontext)) { return(Dwreturn =GetLastError ()); } Hcrypthash Hhash; //ALG Id:calg_md5,calg_sha if( ! Cryptcreatehash (Hprov, Algid,0,0, &Hhash)) {Dwreturn=GetLastError (); CryptReleaseContext (Hprov,0 ); returnDwreturn; } if( ! Crypthashdata (Hhash, pbdata, Dwdatalen,0) ) {Dwreturn=GetLastError (); Cryptdestroyhash (Hhash); CryptReleaseContext (Hprov,0 ); returnDwreturn; } DWORD dwsize; DWORD Dwlen=sizeof(dwsize); Cryptgethashparam (Hhash, Hp_hashsize, (BYTE*) (& dwsize), & Dwlen,0 ); BYTE* Phash =Newbyte[dwsize]; Dwlen=dwsize; Cryptgethashparam (Hhash, Hp_hashval, Phash,& Dwlen,0 ); lstrcpy (Pszhash, _t ("" ) ); TCHAR sztemp[3 ]; for(DWORD i =0; i < Dwlen; ++i) {//wsprintf (sztemp, _t ("%x%x"), Phash[i] >> 4, Phash[i] & 0xf);wsprintf (Sztemp, L"%02x", phash[i]); Lstrcat (Pszhash, sztemp); } Delete[]phash; Cryptdestroyhash (Hhash); CryptReleaseContext (Hprov,0 ); returnDwreturn;}void__fastcall Tform2::button2click (TObject *Sender) { Charszstr[ -] = {0 } ; TCHAR szhash[ A] = {0 } ; strcpy (SZSTR,"ww");//wcscpyGethash ((BYTE *) Szstr, strlen (SZSTR), CALG_MD5, Szhash); Caption=Szhash; strcpy (SZSTR,"ww");//wcscpyGethash ((BYTE *) Szstr, strlen (SZSTR), CALG_MD5, Szhash); Caption=Szhash; // //strwcpy (szstr, "Test Sha"); //Gethash ((BYTE *) szstr, strlen (SZSTR), Calg_sha, Szhash); //MessageBox (NULL, Szhash, Szstr, MB_OK);}
C++builder CryptoAPI MD5