Well, here's something I prepared earlier. The source of my program is here:
Code:
#include <stdio.h>
#include <string.h>
void hex_dump(unsigned char *, int);
int main(void)
{
char thing_to_md5[] = "Hello, world!";
unsigned char result[16];
librad_md5_calc(result, thing_to_md5, strlen(thing_to_md5));
printf("md5 of %s is ", thing_to_md5);
hex_dump(result, 16);
printf("\n");
return 0;
}
void hex_dump(unsigned char *s, int sz)
{
int i;
for (i = 0; i < sz; i++)
printf("%02x", s[i]);
}
The key function is librad_md5_calc(unsigned char *output, unsigned char *input, unsigned int inputlen).
output is a pointer to the buffer you want the result to go into, which will be 16 bytes. input is a pointer to the input buffer, and inputlen is the amount of bytes to read of the input buffer. This is because md5 works on arbitrary binary data, not just null terminated strings.
To convert the result from a 16 byte unsigned char array to the canonical ascii representation of an md5sum, I have used a simple hex_dump function to illustrate.
Of course, I have called one md5 implementation, which is too long to post here. I have attached md5.c and md5.h, the above program was appended to the bottom of md5.c, but for your own use, you should of course keep your normal code separate from md5.c
My output:
Code:
md5 of Hello, world! is 6cd3556deb0da54bca060b4c39479839
To confirm it's correct using the system md5sum utility:
Code:
echo -n 'Hello, world!' | md5sum
6cd3556deb0da54bca060b4c39479839 -