HI I have written a C program to detect an MD5 collision detection. I am used to C and pointers etc so the program is quite small. However, no matter how many time I port to C# it doesn't work. my C code is:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <error.h>
#include <openssl/md5.h>
void print_hex(unsigned char *cs, int len)
{
for (int i = 0; i < len; i++)
printf("%02x", cs[i]);
printf("\n");
}
void read_hex_file(char *path, size_t nmemb, unsigned char *buf)
{
FILE *fd = fopen(path, "r");
if (!fd) error(EXIT_FAILURE, errno, "read_hex_file()");
unsigned int x;
for (int i = 0; i < nmemb; i++) {
fscanf(fd, "%02x", &x);
buf[i] = (unsigned char)(0xFF & x);
}
fclose(fd);
}
int main(int argc, char *argv[])
{
if (argc != 3) {
fprintf(stderr, "Invalid args, %s FILE FILE", argv[0]);
return EXIT_FAILURE;
}
unsigned char input[128], r1[MD5_DIGEST_LENGTH], r2[MD5_DIGEST_LENGTH];
read_hex_file(argv[1], sizeof(input), input);
MD5(input, sizeof(input), r1);
read_hex_file(argv[2], sizeof(input), input);
MD5(input, sizeof(input), r2);
print_hex(r1, sizeof(r1));
print_hex(r2, sizeof(r2));
if (strncmp((const char *)r1, (const char *)r2, sizeof(r1)) == 0)
printf("Hashes match\n");
else
printf("Hashes differ\n");
return EXIT_SUCCESS;
}
Am getting absolutely nowhere with C# (which I want to learn) so wondering if it even possible or has it been solved elsewhere... I have looked. The two files contain the different strings which produce the same hash value. I got the data from them here:
http://en.wikipedia.org/wiki/MD5<ahref target="_blank" title="New Window">^]
and they produce the same hash 79054025255fb1a26e4bc422aef54eb4
Any help welcome. Thanks.