Neo
Neo

Reputation: 349

Getting hash of a binary file C

I want to get hash of a binary file whose name I have. I have tried the following, but then realized that SHA1() is returning hash value for the string ( name of the file). But I want to run it on the file itself. Any pointers on how to do that would be great.

char *fileName = "/bin/ls"
unsigned char hash[SHA_DIGEST_LENGTH];
SHA1((unsigned char *)fileName, strlen(fileName),hash);

Upvotes: 2

Views: 5203

Answers (4)

Neo
Neo

Reputation: 349

Thanks to everyone's comments I solved the problem. I am posting the code here, so others might find it beneficial.

void getFileHash(char *fileName){

    unsigned char result[2*SHA_DIGEST_LENGTH];
    unsigned char hash[SHA_DIGEST_LENGTH];
    int i;
    FILE *f = fopen(fileName,"rb");
    SHA_CTX mdContent;
    int bytes;
    unsigned char data[1024];

    if(f == NULL){
        printf("%s couldn't open file\n",fileName);
        exit(1);
    }

    SHA1_Init(&mdContent);
    while((bytes = fread(data, 1, 1024, f)) != 0){

        SHA1_Update(&mdContent, data, bytes);
    }
    
    SHA1_Final(hash,&mdContent);

    for(i=0;i<SHA_DIGEST_LENGTH;i++){
        printf("%02x",hash[i]);
    }
    printf("\n");
    /** if you want to see the plain text of the hash */
    for(i=0; i < SHA_DIGEST_LENGTH;i++){
        sprintf((char *)&(result[i*2]), "%02x",hash[i]);
    }

    printf("%s\n",result);

    fclose(f);
}

Upvotes: 7

Sandro Kalatozishvili
Sandro Kalatozishvili

Reputation: 602

You need just give argument of file context to SHA1(). The variable fileName contains string "/bin/ls" and SHA1() function returns hash of that string. Here is simple example how to read file and get hash

   /* Used variables */
   FILE *fp;
   char string[2048];
   unsigned char hash[SHA_DIGEST_LENGTH];
   int t;

   /* Open file */
   fp=fopen("test","r");
   if (fp == NULL)
   {
      printf("Can not open file\n");
      exit(1);
   }

   /* Read file */
   fread (string,1,sizeof(string),fp);

   /* Get hash of file */
   SHA1((unsigned char *)string, strlen(string),hash);

I hope buffer size of string (2048) will be enough for the file context.

Upvotes: 0

n. m. could be an AI
n. m. could be an AI

Reputation: 120059

You need to read the file chunk by chunk, and compute the digest chunk by chunk.

Read chunks up to e.g. 2048 bytes with fread. Use SHA1_Init in the beginning, SHA1_Update for each chunk, and SHA1_Final in the end.

You can use plain SHA1 function if you read the entire file in one gulp, but this is not recommended.

Another method is to memory-map the file (see mmap() or whatever the Windows equivalent is) and use plain SHA1. This method is very efficient but less portable than the other one.

Upvotes: 1

Oleksandr Kravchuk
Oleksandr Kravchuk

Reputation: 6327

I don't know how your SHA1() function works (is it from libssl?), but I assume that by

SHA1((unsigned char *)fileName, strlen(fileName),hash);

you are hashing file name, so /bin/ls string. You need to read file content byte by byte into a buffer and hash it.

Upvotes: 1

Related Questions