Hi
I am trying to get an sha-1 for a number of files. What I currently do is cycle the files in a given path, open and read each file separately and load the contents in a buffer and then send it to openssl's SHA function to get the hash. The code looks something like this:
void ReadHashFile(LPCTSTR name)
{
FILE * pFile;
long lSize;
char * buffer;
size_t result;
pFile = _tfopen ( name , L"rb" );
if (pFile==NULL) {fputs ("File error",stderr); return;}
// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
if(lSize == -1){fputs ("Read Error",stderr);return;}
// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {fputs ("Memory error",stderr); return;}
// copy the file into the buffer:
result = fread (buffer,1,lSize,pFile);
if (result != lSize) {fputs ("Reading error",stderr); return;}
/* the whole file is now loaded in the memory buffer. */
// terminate
fclose (pFile);
//Do what ever with buffer
unsigned char ibuf[] = "compute sha1";
unsigned char obuf[20];
SHA1((const unsigned char*)buffer, strlen((const char*)buffer), obuf);
fwprintf(stderr, L"file %s\n", name);
int i;
for (i = 0; i < 20; i++) {
printf("%02x ", obuf[i]);
}
printf("\n");
free(buffer);
}
Some files seem to be unreadable, some give me a -1 size others I can only read the first 2-3 bytes which gives a lot of files the same sha even though they are different.
I would appreciate it if someone can help me with this or if anyone has experience in file hashing. Oh and is there a way of getting a file's sha1 without loading the entire file in memory first, I mean considering large files, this solution wont work.
Regards