我发现了一些由以下原型组成的md5代码......
我一直在试图找出我要放置我想要哈希的字符串的位置,我需要调用哪些函数,以及在经过哈希处理后找到字符串的位置.我对uint32 buf [4]和uint32位[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]);
Run Code Online (Sandbox Code Playgroud)
Chr*_*rle 38
我不知道这个特殊的库,但我使用了非常类似的调用.所以这是我最好的猜测:
unsigned char digest[16];
const char* string = "Hello World";
struct MD5Context context;
MD5Init(&context);
MD5Update(&context, string, strlen(string));
MD5Final(digest, &context);
Run Code Online (Sandbox Code Playgroud)
这将返回哈希的整数表示.如果要将其作为字符串传递,则可以将其转换为十六进制表示形式.
char md5string[33];
for(int i = 0; i < 16; ++i)
sprintf(&md5string[i*2], "%02x", (unsigned int)digest[i]);
Run Code Online (Sandbox Code Playgroud)
tod*_*odd 35
这是一个完整的例子:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(__APPLE__)
# define COMMON_DIGEST_FOR_OPENSSL
# include <CommonCrypto/CommonDigest.h>
# define SHA1 CC_SHA1
#else
# include <openssl/md5.h>
#endif
char *str2md5(const char *str, int length) {
int n;
MD5_CTX c;
unsigned char digest[16];
char *out = (char*)malloc(33);
MD5_Init(&c);
while (length > 0) {
if (length > 512) {
MD5_Update(&c, str, 512);
} else {
MD5_Update(&c, str, length);
}
length -= 512;
str += 512;
}
MD5_Final(digest, &c);
for (n = 0; n < 16; ++n) {
snprintf(&(out[n*2]), 16*2, "%02x", (unsigned int)digest[n]);
}
return out;
}
int main(int argc, char **argv) {
char *output = str2md5("hello", strlen("hello"));
printf("%s\n", output);
free(output);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
正如其他答案所提到的,以下调用将计算哈希值:
MD5Context md5;
MD5Init(&md5);
MD5Update(&md5, data, datalen);
MD5Final(digest, &md5);
Run Code Online (Sandbox Code Playgroud)
将其拆分为许多函数的目的是让您流式传输大型数据集.
例如,如果你正在对一个10GB的文件进行哈希处理并且它不适合ram,那么你可以这样做.您将以较小的块读取文件并调用MD5Update它们.
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);
Run Code Online (Sandbox Code Playgroud)
说实话,原型附带的评论似乎很清楚.像这样的东西应该做的伎俩:
void compute_md5(char *str, unsigned char digest[16]) {
MD5Context ctx;
MD5Init(&ctx);
MD5Update(&ctx, str, strlen(str));
MD5Final(digest, &ctx);
}
Run Code Online (Sandbox Code Playgroud)
str你想要哈希的C字符串在哪里,并且digest是得到的MD5摘要.
| 归档时间: |
|
| 查看次数: |
111986 次 |
| 最近记录: |