Better, but still could use some work. Witness:
Code:
#include<stdio.h>
#include<string.h>
#include<dirent.h>
#include<openssl/evp.h>
#include<openssl/hmac.h>
int main (int argc , char ** argv )
{
struct dirent* dent;
char key [] = " This is the HMAC key";
unsigned char result [ EVP_MAX_MD_SIZE ];
char * data ;
int i, size , *len ;
DIR* dir=opendir("..");
FILE *fp;
/* Open input file */
if ( argc != 2)
{
fprintf (stderr , " Usage : %s filename \n", argv [0]);
abort ();
}
fp = fopen ( argv [1] , "rb");
if (! fp)
{
fprintf (stderr , " Cannot open file %s\n", argv [1]);
abort ();
}
/* Read file into the data buffer */
if(dir)
{
while((dent=readdir(dir)))
{
fseek (fp , 0, SEEK_END );
size = ftell (fp );
rewind (fp );
data = ( char *) malloc ( size );
fread (data , 1, size , fp );
fclose (fp );
/* Compute HMAC */
HMAC ( EVP_sha256 (), key , strlen ( key), data , size , result , len );
}
closedir(dir);
}
/* Output result */
printf (" The HMAC of file %s is: ", argv [1]);
for (i = 0; i < *len ; i++)
printf (" %02x", result [i ]);
printf ("\n");
return 0;
}
I don't know the HMAC function, but I would guess your problem is that len doesn't actually point anywhere. Most likely, the HMAC function wants a pointer to an integer, so it can pass back the length of something via the len parameter. You probably need to declare len as a plain integer, then pass in it's address:
Code:
int len;
...
HMAC(..., &len);
Also, I don't know if this matters, but does data need to be null terminated? If so, you need to malloc size+1 bytes, and set data[size] = '\0'.