我发现了一些包含以下原型(prototype)的md5代码...
我一直在尝试找出必须将要散列的字符串放入何处,需要调用哪些函数以及在对字符串进行散列后在何处查找。我对uint32 buf [4]和uint32 bits [2]在结构中的内容感到困惑。
struct MD5Context {
uint32 buf[4];
uint32 bits[2];
unsigned char in[64];
};
/*
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
* initialization constants.
*/
void MD5Init(struct MD5Context *context);
/*
* Update context to reflect the concatenation of another buffer full
* of bytes.
*/
void MD5Update(struct MD5Context *context, unsigned char const *buf, unsigned len);
/*
* Final wrapup - pad to 64-byte boundary with the bit pattern
* 1 0* (64-bit count of bits processed, MSB-first)
*/
void MD5Final(unsigned char digest[16], struct MD5Context *context);
/*
* The core of the MD5 algorithm, this alters an existing MD5 hash to
* reflect the addition of 16 longwords of new data. MD5Update blocks
* the data and converts bytes into longwords for this routine.
*/
void MD5Transform(uint32 buf[4], uint32 const in[16]);
最佳答案
我不知道这个特定的库,但是我使用了非常相似的调用。所以这是我最好的猜测:
unsigned char digest[16];
const char* string = "Hello World";
struct MD5Context context;
MD5Init(&context);
MD5Update(&context, string, strlen(string));
MD5Final(digest, &context);
这将为您提供哈希的整数表示。如果要将其作为字符串传递,则可以将其转换为十六进制表示形式。
char md5string[33];
for(int i = 0; i < 16; ++i)
sprintf(&md5string[i*2], "%02x", (unsigned int)digest[i]);
关于c - 如何在C中创建字符串的md5哈希?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7627723/