Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

visual studio 2010 - computing hashValue of a file

  1. Small piece of code I wrote to list files in a dummy directory and then calculate the hash values.
  2. But when I pass the filename to the calc_hash function , program terminates after computing the hash of two files... with error Unhandled exception at 0x77406850 in hashtest3-cpp.exe: 0xC0000005: Access violation writing location 0x64333782.

Can someone please point out the error .... code snippet::

#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
#include <strsafe.h>
#include <string.h>
#include <tchar.h>
#include <iostream>

using namespace std;

#define BUFSIZE 1024
#define MD5LEN  16

//======================================================VARIABLE INITIALIZATION
DWORD Status = 0;
BOOL bResult = FALSE;

ULONG_PTR hProv = 0;
ULONG_PTR hHash = 0;
HANDLE hFile = NULL;
DWORD i;
DWORD j=0;


BYTE rgbFile[BUFSIZE];
DWORD cbRead = 0;
BYTE rgbHash[MD5LEN];
DWORD cbHash = 0;
LPTSTR filename;
CHAR resultstream[33];

HANDLE hFind;
WIN32_FIND_DATA data;

TCHAR filepath[260] = _T("");
LPWSTR FNAME;
LPWSTR FULLPATH = L"c:\test";
LPWSTR dir = L"c:\test\*.*";
LPWSTR DEFAULt = L"";

CHAR rgbDigits[] = "0123456789abcdef";


//=====================================================FUNCTION DECLARATION

void list_files(void);

void calc_hash(void);


//======================================================MAIN START

int main()
{
list_files();
//calc_hash();





getchar();
return 0; 
}


//======================================================LISTING FILES IN A DIRECTORY

void list_files()
{
    hFind = FindFirstFile(dir, &data);
if (hFind != INVALID_HANDLE_VALUE)
{

    do 
    {

        StringCchCopy(filepath, 260, DEFAULt);
        resultstream[33] = '';
        DWORD j=0;

        FNAME = data.cFileName;
        StringCchCat(filepath, 260, FULLPATH);
        StringCchCat(filepath, 260, FNAME);
        filename = (LPTSTR)filepath;

        printf("
%ws", filename);

        /*calc_hash();
        getchar();*/

        continue;

    }while (FindNextFile(hFind, &data));

}
getchar();
}

//======================================================HASH OF A FILE
void calc_hash()
{

hFile = CreateFile(
        filename,
        GENERIC_READ,
        FILE_SHARE_READ,
        NULL,
        OPEN_EXISTING,
        FILE_FLAG_SEQUENTIAL_SCAN,
        NULL
        );

if (INVALID_HANDLE_VALUE == hFile)
{
    Status = GetLastError();
//printf("
error opening file::%d", Status);

goto end;
}

// Get handle to the crypto provider
if (!CryptAcquireContext(   
            &hProv,
            NULL,
            NULL,
            PROV_RSA_FULL,
            CRYPT_VERIFYCONTEXT))
{
    Status = GetLastError();
    printf("CryptAcquireContext error:: %d
", Status); 
    CloseHandle(hFile);
getchar();
goto end;

}

if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
{
    Status = GetLastError();
    printf("CryptAcquireContext error:: %d
", Status); 
    CloseHandle(hFile);
    CryptReleaseContext(hProv, 0);
getchar();
goto end;

}

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

    if (!CryptHashData(hHash, rgbFile, cbRead, 0))
    {
        Status = GetLastError();
        printf("CryptHashData error:: %d
", Status); 
        CryptReleaseContext(hProv, 0);
        CryptDestroyHash(hHash);
        CloseHandle(hFile);
    getchar();
    goto end;

    }
}

//================================GENERATING RESULT=============================
if (!bResult)
{
    Status = GetLastError();
    printf("ReadFile error:%S:: %d
",filename, Status); 
    CryptReleaseContext(hProv, 0);
    CryptDestroyHash(hHash);
    CloseHandle(hFile);
getchar();
goto end;

}

cbHash = MD5LEN;

if (CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0))
{
    printf("MD5 hash of file %ws is:- ", filename);
    for (i = 0; i < cbHash; i++)
    {
    resultstream[j]=rgbDigits[rgbHash[i] >> 4];
    j++;
    resultstream[j]=rgbDigits[rgbHash[i] & 0xf];
    j++;


    }
resultstream[j] = ''; 
printf("
%s", resultstream );
    printf("
");

}
else
{
    Status = GetLastError();
    printf("CryptGetHashParam error:: %d
", Status); 
}

CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
CloseHandle(hFile);




end:;

//getchar();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is you declare resultstream as

CHAR resultstream[33];

but then you use in your code

resultstream[33] = '';

The resultstream array is 0 indexed so valid values are 0-32, you are accessing memory not allocated for that array (hence the "Access violation")


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...