cfilesha1

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);

Solution

  • 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);
    }