473,657 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert output from MD5 to printable ascii

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
749226c29c17114 562745d9769fdab 45 -
[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);
}

Feb 23 '07 #1
19 11401
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.

Feb 23 '07 #2
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
749226c29c17114 562745d9769fdab 45 -
[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
Feb 23 '07 #3
On Feb 23, 10:49 am, Daniel Rudy <spamt...@spamt his.netwrote:
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.

Feb 23 '07 #4
On Feb 23, 11:44 am, "Serman D." <serma...@hotma il.comwrote:
How do I copy the contents to a string (unsigned char pointer)
variable instead of printing to stdout?
sprintf()

Feb 23 '07 #5
At about the time of 2/23/2007 3:44 AM, Serman D. stated the following:
On Feb 23, 10:49 am, Daniel Rudy <spamt...@spamt his.netwrote:
>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
Feb 23 '07 #6
At about the time of 2/23/2007 3:44 AM, Serman D. stated the following:
On Feb 23, 10:49 am, Daniel Rudy <spamt...@spamt his.netwrote:
>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(md5asci i, 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
Feb 23 '07 #7
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(md5asci i, 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(md5asci i, c, sizeof(md5ascii ) - 1);
}

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

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

Feb 23 '07 #8
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(md5asci i, 0, sizeof(md5ascii );
for (i = 0; i < 16; i++)
{
snprintf(c, sizeof(c), "%0x", pout[i];
strncat(md5asci i, 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(md5asci i, c, sizeof(md5ascii ) - 1);
}

for(i = 0; i < sizeof(md5ascii ); i++) putchar(md5asci i[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
Feb 24 '07 #9
On Feb 23, 1:49 am, Daniel Rudy <spamt...@spamt his.netwrote:
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
749226c29c17114 562745d9769fdab 45 -
[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++ = "0123456789ABCD EF"[(pout[i] >4) & 0xf];
*p++ = "0123456789ABCD EF"[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/

Feb 24 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5464
by: Daniel Alexandre | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi there, I'm using the following method in my program to check whether a message received is printable or not and to strip the non-printable characters: CheckPrintable(self,message): printablemessage = ""
5
25650
by: Joseph | last post by:
Hi all, I was wondering if there is any lib or function could do the following things: input: a char output: 8 digits binary presentation for that given char Example:
3
5849
by: Java and Swing | last post by:
I have some output stored in a string that looks like.. >> x '\x01\xee\x1eo\xc3+\x8b\x83\xfad\xf6E\xaa\x0ea/I\x96\x83\xf5G\xa3\rQ\xfcH\xee\r' According to, http://docs.python.org/lib/typesseq-strings.html, this is "Unsigned Hexidecimal (lowercase)". How can I get this into normal Ascii so that I can read it?
3
1736
by: danmc91 | last post by:
Hi, I'm just getting going with xml and xslt. I'm trying to write what are essentially man pages and I need 3 output formats. 1) nroff -man format for real man pages 2) html for an online help browser 3) ascii where every line must start with a '%' character (octave
1
4440
by: Tim Menninger | last post by:
When I use the XslTransform.Transform method to write to the HttpRequest.OutputStream The first byte of the output is always an invalid character, looks like an ascii zero or some other non-printable ascii char. The problem is that it messes up my display. Here is the code: .... Response.Write("<span>some text</span>"); XmlTextReader reader = new XmlTextReader(xslFile); XslTransform xsl = new XslTransform();
11
5200
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 The program listed below demonstrates the use of wcsftime() and std::time_put<wchar_t> which is a C++ wrapper around it. (I know this isn't C; but the "problem" lies in the C library implementation of wcsftime()). I'm not sure if this is a platform-dependent feature or part of the C standard. I've compiled with GCC 3.4.3 on GNU/Linux, and run in an en_GB UTF-8
4
25055
by: meendar | last post by:
Hi, I am having a character pointer which contains ascii values. i just want to convert all these ascii values to respective characters and again store it in another character pointer. Anybody please help in c language. Thanks in Advance.
0
3416
by: Michael Postmann | last post by:
Hello! I have Oracle 10g using AL32UTF8 as internal charset. I have a documents table where uploaded files (I'm using modplsql web-gateway (release 8i)) are stored in a blob-column. I upload a CSV-File to this table which is in ISO 8859-1 Charset. I now need to convert this binary data (which is in fact all character-data) into utf-8.
19
5324
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that str(object) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given, returns the empty string, ''.
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8730
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1607
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.