Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Convert output from MD5 to printable ascii

Reply
Thread Tools

Convert output from MD5 to printable ascii

 
 
Serman D.
Guest
Posts: n/a
 
      02-23-2007
Hi,

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 from http://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);
}

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      02-23-2007
Serman D. wrote:
> Hi,
>
> 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.


Use printf with the %x or %X format specifier. Access the hash as an
array of unsigned char. Also the md5sum library may itself provide
formatting routines.

 
Reply With Quote
 
 
 
 
Daniel Rudy
Guest
Posts: n/a
 
      02-23-2007
At about the time of 2/23/2007 12:58 AM, Serman D. stated the following:
> Hi,
>
> 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 from http://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");

I use 16 because MD6 hashes are 128 bits or 16 bytes long. This is
assuming an Intel platform.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
Reply With Quote
 
Serman D.
Guest
Posts: n/a
 
      02-23-2007
On Feb 23, 10:49 am, Daniel Rudy <(E-Mail Removed)> wrote:
> At about the time of 2/23/2007 12:58 AM, Serman D. stated the following:
> > 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");


Thanks Daniel,

Follow-up question:

How do I copy the contents to a string (unsigned char pointer)
variable instead of printing to stdout?

I'm completely lost in the C jungle of string manipulation..

--
Serman D.

 
Reply With Quote
 
anyone.anon@googlemail.com
Guest
Posts: n/a
 
      02-23-2007
On Feb 23, 11:44 am, "Serman D." <(E-Mail Removed)> wrote:
> How do I copy the contents to a string (unsigned char pointer)
> variable instead of printing to stdout?


sprintf()

 
Reply With Quote
 
Daniel Rudy
Guest
Posts: n/a
 
      02-23-2007
At about the time of 2/23/2007 3:44 AM, Serman D. stated the following:
> On Feb 23, 10:49 am, Daniel Rudy <(E-Mail Removed)> wrote:
>> At about the time of 2/23/2007 12:58 AM, Serman D. stated the following:
>>> 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");

>
> Thanks Daniel,
>
> Follow-up question:
>
> How do I copy the contents to a string (unsigned char pointer)
> variable instead of printing to stdout?
>
> I'm completely lost in the C jungle of string manipulation..
>
> --
> Serman D.
>


The BIG thing you need to realize here is that strings in C are null
terminated (ASCII 0). The output of MD5 is BINARY, so for an unsigned
char, all values 0-255 are valid data...this includes the 0 or null char
for string termination. There's a set of functions that is used to
handle binary strings. To copy binary strings, take a look at memcpy or
memmove.

Something like:

unsigned char dst[16];
memcpy(dst, pout, 16);

Since the data is binary, there is no terminating null. You just have
to be careful when working with it and use the size correctly. The best
way to do this would be like this:

#include <stdio.h>
#include <string.h>
#include "md5.h"

#define MD5HASHSIZE 16

int main (void) {

unsigned char in[64] = "This is my dearest secret: 12345";
unsigned char *pout;
unsigned char dest[MD5HASHSIZE];
int i;

// compute MD5 hash
md5_csum(in, 64, pout);

printf("\"%s\"\n", pout);

for (i = 0; i < MD5HASHSIZE; i++) printf("%0x", pout[i]);
printf("\n");

memmove(dest, pout, MD5HASHSIZE);

return(0);
}

Note the #define directive. The pre-processor will replace all
instances of MD5HASHSIZE with 16. So if you change the value of
MD5HASHSIZE, it will change everywhere so you don't have to go through
your code and change each instance manually. Think of it as a constant.
It's not a const, but it's like a const.

I'm sure that someone will correct me on this....
--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
Reply With Quote
 
Daniel Rudy
Guest
Posts: n/a
 
      02-23-2007
At about the time of 2/23/2007 3:44 AM, Serman D. stated the following:
> On Feb 23, 10:49 am, Daniel Rudy <(E-Mail Removed)> wrote:
>> At about the time of 2/23/2007 12:58 AM, Serman D. stated the following:
>>> 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");

>
> Thanks Daniel,
>
> Follow-up question:
>
> How do I copy the contents to a string (unsigned char pointer)
> variable instead of printing to stdout?
>
> I'm completely lost in the C jungle of string manipulation..
>
> --
> Serman D.
>


Disreguard my other post. I misunderstood what you were refering too.

int i;
char c[10];
char md5ascii[128];

memset(md5ascii, 0, sizeof(md5ascii);
for (i = 0; i < 16; i++)
{
snprintf(c, sizeof(c), "%0x", pout[i];
strncat(md5ascii, c, sizeof(md5ascii) - 1;
}



--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      02-23-2007
Daniel Rudy wrote:
> At about the time of 2/23/2007 3:44 AM, Serman D. stated the following:


<snip>

> > Follow-up question:
> >
> > How do I copy the contents to a string (unsigned char pointer)
> > variable instead of printing to stdout?

[ ... ]

> Disreguard my other post. I misunderstood what you were refering too.
>
> int i;
> char c[10];
> char md5ascii[128];
>
> memset(md5ascii, 0, sizeof(md5ascii);
> for (i = 0; i < 16; i++)
> {
> snprintf(c, sizeof(c), "%0x", pout[i];
> strncat(md5ascii, c, sizeof(md5ascii) - 1;
> }


Hacked out on the spot eh? You've forgotten to close the argument list
for memset, snprintf and strncat. Required headers, definition of pout
and main are missing. Here's a compilable example based on yours:

#include <stdio.h>
#include <string.h>

int i;
char c[10];
char md5ascii[128];
unsigned char pout[16];

int main(void) {
for(i = 0; i < sizeof(pout); i++) pout[i] = i + 1;

memset(md5ascii, 0, sizeof(md5ascii));
for (i = 0; i < 16; i++) {
snprintf(c, sizeof(c), "%0x", pout[i]);
strncat(md5ascii, c, sizeof(md5ascii) - 1);
}

for(i = 0; i < sizeof(md5ascii); i++) putchar(md5ascii[i]);

putchar('\n');
return 0;
}

 
Reply With Quote
 
Daniel Rudy
Guest
Posts: n/a
 
      02-24-2007
At about the time of 2/23/2007 10:05 AM, santosh stated the following:
> Daniel Rudy wrote:
>> At about the time of 2/23/2007 3:44 AM, Serman D. stated the following:

>
> <snip>
>
>>> Follow-up question:
>>>
>>> How do I copy the contents to a string (unsigned char pointer)
>>> variable instead of printing to stdout?

> [ ... ]
>
>> Disreguard my other post. I misunderstood what you were refering too.
>>
>> int i;
>> char c[10];
>> char md5ascii[128];
>>
>> memset(md5ascii, 0, sizeof(md5ascii);
>> for (i = 0; i < 16; i++)
>> {
>> snprintf(c, sizeof(c), "%0x", pout[i];
>> strncat(md5ascii, c, sizeof(md5ascii) - 1;
>> }

>
> Hacked out on the spot eh?


Pretty much. That's what happens when you are up since 1 in the AM.

> You've forgotten to close the argument list
> for memset, snprintf and strncat. Required headers, definition of pout
> and main are missing. Here's a compilable example based on yours:
>
> #include <stdio.h>
> #include <string.h>
>
> int i;
> char c[10];
> char md5ascii[128];
> unsigned char pout[16];
>
> int main(void) {
> for(i = 0; i < sizeof(pout); i++) pout[i] = i + 1;
>
> memset(md5ascii, 0, sizeof(md5ascii));
> for (i = 0; i < 16; i++) {
> snprintf(c, sizeof(c), "%0x", pout[i]);
> strncat(md5ascii, c, sizeof(md5ascii) - 1);
> }
>
> for(i = 0; i < sizeof(md5ascii); i++) putchar(md5ascii[i]);
>
> putchar('\n');
> return 0;
> }
>


Opps.

Looks like my secret is out. I guess you can tell that I rely on
compiler warnings and errors *way* too much.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
Reply With Quote
 
websnarf@gmail.com
Guest
Posts: n/a
 
      02-24-2007
On Feb 23, 1:49 am, Daniel Rudy <(E-Mail Removed)> 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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
create a md5 / md5 passwd with a salt Peter Woodsky Ruby 6 11-21-2008 09:08 AM
md5 from python different then md5 from command line ursache.marius@gmail.com Python 9 05-07-2006 11:49 PM
[FR/EN] how to convert the characters ASCII(0-255) to ASCII(0-127) Alextophi Perl Misc 8 12-30-2005 10:43 AM
Question about string.printable and non-printable characters Daniel Alexandre Python 2 03-21-2005 12:34 PM
I remember someone asking about an MD5 javascript: http://pajhome.org.uk/crypt/md5/ Mozzie \( v \) Javascript 0 07-12-2004 01:06 PM



Advertisments