我正在使用L Peter Deutsch从页面和sourceforge.net的源代码中找到的 C 语言中的 MD5 实现。
该文件的main
函数中的代码md5main.c
已修改为我使用如下:
md5_byte_t *result = malloc ( 16 ); // Allocating 128 bits.
char data[] = "my name is aman";
md5_state_t state; // This is a state object used by the MD5 library to do "stuff".
// Just treat it as a black box.
md5_init(&state);
md5_append(&state, (const md5_byte_t *)data, strlen(data));
md5_finish(&state, result);
print_bits(result, 128);
md5_append(&state, (const md5_byte_t *)data, strlen(data));
md5_finish(&state, result);
print_bits(result, 128);
//puts(usage);
return 0;
打印位函数如下:
void print_bits ( void * data, int length )
{
uint8_t *temp_data = (uint8_t *)data;
int bytes = length>>3;
printf ("[");
int i;
//printf("\nprinting bloom filter\n\n");
for ( i=0; i<bytes; ++i )
{
int j;
for ( j=sizeof(uint8_t)*8-1; j>=0; --j)
{
int l = temp_data[i]>>j & 1;
printf( "%d", l);
}
}
//printf("\n end printing bloom filter\n\n");
printf ("]\n");
}
您将需要包含stdlib.h
和代码。
现在对于字符串 the my name is aman
,它在调用两次时产生了两个不同的哈希值。它在我的系统上产生了以下内容:
[01110000100000100110101111101101000011000100011100110111010100110010011100110011111100100111100100100011101011000111110011010101]
[11101001101110101111000010101100000000011011010110110011111001111010110111001011000001010000010111000010101111101101000010011000]
这种奇怪行为的原因是什么?它不应该为特定键产生相同的哈希值吗?