braindump
braindump

Reputation: 309

C: MD5 gives garbage as result

I'm using Peter Deutsch's implementation of MD5 to implement a simple password check. I use it this way:

md5_state_t md;
char *in = "Hello World";
char *out[16];

md5_init(&md);
md5_append(&md, in, strlen(in));
md5_finish(&md, out);

printf("In:  %s\n", in);
printf("Out: %s\n", out);

Problem is, that I get a result like this:

In:  Hello World
Out: ?
??d?uA????.??

Does anyone has an idea whats going wrong here?

Upvotes: 0

Views: 222

Answers (1)

nos
nos

Reputation: 229274

An MD5 hash is a binary blob of 16 bytes. You cannot print that as a string. Print it e.g. in a hex representation:

md5_state_t md;
char *in = "Hello World";
char out[16];
int i;

md5_init(&md);
md5_append(&md, in, strlen(in));
md5_finish(&md, out);

printf("In:  %s\n", in);
printf("Out: ");
for(i = 0; i < 16: i++) 
   printf("%02X", out[i]);
puts("");

Note that the above changes out to be a char as well, it can not be a char*

Upvotes: 3

Related Questions