On Feb 23, 1:49 am, Daniel Rudy <spamt...@spamthis.net> wrote:
> At about the time of 2/23/2007 12:58 AM, Serman D. stated the
> following:
> > I have very limited C knowledge. I want to convert to output from a
> > MD5 hash algorithm to printable ascii similar to the output of the
> > md5sum in GNU coreutils. Any help on how to do the conversion is
> > appreciated.
>
> > [aff@afflinux md5_xyssl]$ gcc -o test test.c md5.c
> > [aff@afflinux md5_xyssl]$ ./test
> > "J?n??CBW? ?}"
> > [aff@afflinux md5_xyssl]$ echo "This is my dearest secret: 12345" |
> > md5sum
> > 749226c29c17114562745d9769fdab45 -
> > [aff@afflinux md5_xyssl]$
>
> > $ md5sum --version
> > md5sum (coreutils) 5.2.1
>
> > /*
> > * test.c
> > *
> > * MD5 source fromhttp://xyssl.org/code/source/md5/
> > *
> > */
>
> > #include "md5.h"
> > #include <string.h>
>
> > int main (void) {
>
> > unsigned char in[64] = "This is my dearest secret: 12345";
> > unsigned char *pout;
>
> > // compute MD5 hash
> > md5_csum(in, 64, pout);
>
> > printf("\"%s\"\n", pout);
> > }
>
> int i;
>
> for (i = 0; i < 16; i++) printf("%0x", pout[i]);
> printf("\n");
Let's try:
for (i=0; i < 16; i++) printf ("%02x", pout[i]);
printf ("\n");
The difference kind of matters.
However, I would, of course, just do this:
char out[33], *p;
for (p=out,i=0; i < 16; i++) {
*p++ = "0123456789ABCDEF"[(pout[i] >> 4) & 0xf];
*p++ = "0123456789ABCDEF"[pout[i] & 0xf];
}
*p = '\0';
puts (out);
to avoid linking in all of printf() unnecessarily.
--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/