Getting md5 hash code for dll file in c++

0

Hi guys i am trying to get dll md5 hash but it is returning same value all the time, what i did wrong? this dll is already loaded when i am trying to getHash i am getting hash with getHash() method and calculating it with CalcHash

thnks in advanced.

#define BUFSIZE 1024
#define MD5LEN  16
int CalcHash(HANDLE hFile, char *md5sum)
{
    BOOL bResult = FALSE;
    HCRYPTPROV hProv = 0;
    HCRYPTHASH hHash = 0;
    BYTE rgbFile[BUFSIZE];
    DWORD cbRead = 0;
    BYTE rgbHash[MD5LEN];
    DWORD cbHash = 0;
    CHAR rgbDigits[] = "0123456789abcdef";

    char byt[3];
    int rc, err;

    rc = CryptAcquireContext(&hProv, NULL, MS_STRONG_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);

    if(!rc)
    {
        err = GetLastError();
        if(err==0x80090016)
        {
            //first time using crypto API, need to create a new keyset
            rc=CryptAcquireContext(&hProv, NULL, MS_STRONG_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET);
            if(!rc)
            {
                err=GetLastError();
                return 0;
            }
        }
    }

    CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash);

    while(bResult = ReadFile(hFile, rgbFile, BUFSIZE, &cbRead, NULL))
    {
        if (0 == cbRead)
        {
            break;
        }

        CryptHashData(hHash, rgbFile, cbRead, 0);
    }

    cbHash = MD5LEN;
    CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0);
    md5sum[0] = 0;

    for (DWORD i = 0; i < cbHash; i++)
    {
        sprintf(byt, "%c%c", rgbDigits[rgbHash[i] >> 4], rgbDigits[rgbHash[i] & 0xf]);
        strcat(md5sum, byt);
    }

    CryptDestroyHash(hHash);
    CryptReleaseContext(hProv, 0);
    return 1;
}

char *getHash()
{
    CalcHash(L"dsetup.dll", md5sum);
    Logger(md5sum);
    return md5sum;
} 
c++
hash

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0