如何创建在C字符串的MD5哈希? [英] How to create a md5 hash of a string in C?
问题描述
我已经发现了一些MD5 code,它包含以下原型...
我一直在试图找出我必须把我要散列的字符串,我需要调用什么功能,以及在哪里可以找到字符串一旦被散列。我很困惑与问候什么UINT32 BUF [4]和UINT32位[2]是在结构中。
结构MD5Context {
UINT32的buf [4];
UINT32比特[2];
在无符号的字符[64];
};/ *
*启动MD5积累。设置位计数为0,缓冲到神秘
*初始化常量。
* /
无效MD5Init(结构MD5Context *上下文);/ *
*更新背景下,以反映另一个缓冲区满的串联
*字节。
* /
无效MD5Update(结构MD5Context *的背景下,unsigned char型常量* buf中,无符号LEN);/ *
*最终结束语 - 填充到64字节边界的位模式
* 1 0 *(变位的64位计数,MSB优先)
* /
无效MD5Final(unsigned char型消化[16],结构MD5Context *上下文);/ *
* MD5算法的核心,这改变现有的MD5哈希
*反映增加新的数据的16个长字。 MD5Update块
*数据,并将其转换成字节为长字该例程。
* /
空隙MD5Transform(UINT32的buf [4],UINT32在常量[16]);
我不知道这个特殊的库,但我已经用非常类似的电话。所以这是我最好的猜测:
unsigned char型消化[16];
为const char *字符串=Hello World的;
结构MD5Context语境;
MD5Init(安培;背景);
MD5Update(安培,背景,字符串的strlen(字符串));
MD5Final(消化,&安培;背景);
这会给你回重新散列presentation一个整数。然后,您可以,如果你想围绕把它作为一个字符串转成十六进制再presentation这一点。
字符md5string [33];
的for(int i = 0; I< 16; ++ I)
sprintf的(安培; md5string [我* 2],%02X(无符号整数)摘要[I]);
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 need to call, and where to find the string once it has been hashed. I'm confused with regards to what the uint32 buf[4] and uint32 bits[2] are in the struct.
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]);
I don't know this particular library, but I've used very similar calls. So this is my best guess:
unsigned char digest[16];
const char* string = "Hello World";
struct MD5Context context;
MD5Init(&context);
MD5Update(&context, string, strlen(string));
MD5Final(digest, &context);
This will give you back an integer representation of the hash. You can then turn this into a hex representation if you want to pass it around as a string.
char md5string[33];
for(int i = 0; i < 16; ++i)
sprintf(&md5string[i*2], "%02x", (unsigned int)digest[i]);
这篇关于如何创建在C字符串的MD5哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!