Getting hash of a binary file C

后端 未结 4 1239
清歌不尽
清歌不尽 2021-01-14 21:05

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 o

4条回答
  •  独厮守ぢ
    2021-01-14 21:16

    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

提交回复
热议问题