How to create a md5 hash of a string in C?

前端 未结 6 809
独厮守ぢ
独厮守ぢ 2020-12-07 19:06

I\'ve found some md5 code that consists of the following prototypes...

I\'ve been trying to find out where I have to put the string I want to hash, what functions I

6条回答
  •  攒了一身酷
    2020-12-07 19:44

    As other answers have mentioned, the following calls will compute the hash:

    MD5Context md5;
    MD5Init(&md5);
    MD5Update(&md5, data, datalen);
    MD5Final(digest, &md5);
    

    The purpose of splitting it up into that many functions is to let you stream large datasets.

    For example, if you're hashing a 10GB file and it doesn't fit into ram, here's how you would go about doing it. You would read the file in smaller chunks and call MD5Update on them.

    MD5Context md5;
    MD5Init(&md5);
    
    fread(/* Read a block into data. */)
    MD5Update(&md5, data, datalen);
    
    fread(/* Read the next block into data. */)
    MD5Update(&md5, data, datalen);
    
    fread(/* Read the next block into data. */)
    MD5Update(&md5, data, datalen);
    
    ...
    
    //  Now finish to get the final hash value.
    MD5Final(digest, &md5);
    

提交回复
热议问题