0

我有两个字符串如下 Str1: 1234\099 Str2: 123499

如果我使用 unsigned char *MD5(const unsigned char *d, unsigned long n, unsigned char *md); 在这些字符串上计算 MD5

函数 MD5 是否会计算 Str1 的前 4 个字节的哈希值,还是会采用完整的字符串“1234\099”。

Str1和Str2的MD5是一样还是不一样????

谢谢

4

3 回答 3

3

这完全取决于您为n参数传递的值。

如果您使用strlen()to calculate n,则该MD5()函数将处理数据直到但不包括 null (因为您实际上告诉了它)。

如果您传入正确的长度 for n(我假设是7for Str1),那么MD5()将包括散列中的所有数据,包括空字节和空值之后的数据。

Str1和的哈希值Str2会有所不同(假设您传递的值大于 3 for n)。

于 2012-09-28T07:28:55.470 回答
0

Yes, Michael is right. I executed the below program and it seems that MD5 calculation takes place on second argument irrespective of what is in the string.

#include <stdio.h>
#include <openssl/md5.h>

int main() { <br>
    char *p = NULL;<br>
    char *c = NULL;<br>
    char *q = NULL;<br>

    p = malloc(10*(sizeof(char)));
    strncpy(p, "ABCDEDFGO", 9);
    c = malloc(200*(sizeof(char)));
    memset(c,0, 200);

    p[3] = '\0';

    q = MD5(p, 9, c);
    printf("\n For 9 bytes hash %x", *c);
    q = MD5(p, 4, c);
    printf("\n For 4 bytes hash %x", *c);
    q = MD5(p, 3, c);
    printf("\n For 3 bytes hash %x", *c);

    free(c);
    return (0);
}

For 9 bytes hash ffffffc9
For 4 bytes hash ffffffe5
For 3 bytes hash ffffff90

于 2012-09-28T07:43:56.227 回答
0

const unsigned char *d它是数组的第一个元素,而不是 C 风格的字符串。

所以指定适当的长度(7个元素)(const unsigned char *)"1234\099"你会得到它的MD5到md.

于 2012-09-28T07:26:48.470 回答