MD5 not returning hash for string ending with "."

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

MD5 not returning hash for string ending with "."

kaustubh mendki
Hi,

I don't know if anyone earlier posted similar
message earlier..If so apologise me for repetition.
I am trying a simple program to get MD5 hash of
any string.The program is as follows:

// try.c
#include <stdio.h>
#include "openssl/md5.h"

int main()
{
        unsigned char var[]="k.";
        unsigned char *op;
     
        op = MD5(var,strlen(var),NULL);
        printf("\nHash= %s\tLen= %d",op, strlen(op));
        return 0;
}

I compiled it with: gcc -lcrypto -o try try.c
The above code is returning empty hash with 0 length.
But with strings like ".", "k.a" this code is
working properly.

Can anyone throw light on it?

Thanks in advance,

Kaustubh







               
____________________________________________________
Start your day with Yahoo! - make it your home page
http://www.yahoo.com/r/hs 
 
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: MD5 not returning hash for string ending with "."

Ken Goldman
> Date: Tue, 19 Jul 2005 04:30:03 -0700 (PDT)
> From: kaustubh mendki <[hidden email]>
>
> I am trying a simple program to get MD5 hash of
> any string.The program is as follows:
>
> #include <stdio.h>
> #include "openssl/md5.h"
>
> int main()
> {
>         unsigned char var[]="k.";
>         unsigned char *op;
>      
>         op = MD5(var,strlen(var),NULL);
>         printf("\nHash= %s\tLen= %d",op, strlen(op));
>         return 0;
> }
>
> I compiled it with: gcc -lcrypto -o try try.c
> The above code is returning empty hash with 0 length.
> But with strings like ".", "k.a" this code is
> working properly.
>
> Can anyone throw light on it?

Hashes are not printable C strings.  They are byte arrays.  Thus,
printf and strlen will both fail.

--
Ken Goldman   [hidden email]   914-784-7646
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: MD5 not returning hash for string ending with "."

kaustubh mendki
Hi Ken,

Yes, hash can contain printable as well as
nonprintable chars.As I mentioned this code
was working with other strings and giving
correct hash length(16).To eliminate this
issue, i converted these chars to hex.
Now code look like this:

#include <stdio.h>
#include "openssl/md5.h"

void hexprint(unsigned char *data, int len)
{    
        int i;
        for(i = 0;i < len;i++)
        {    /* "%02x" for lowercase, "%02X" for
uppercase hexidecimal letters */
                printf("%02x", data[i]);
        }
}
 
int main()
{
         unsigned char var[]="k.";
         unsigned char *op;
       
         op = MD5(var,strlen(var),NULL);
         hexprint(op, strlen(op));
         return 0;        
}

Atleast this should work ..but its not
working either for string "k.". For any
other string working perfectly.

Wonder if its a bug!

Thanks,
Kaustubh


--- Ken Goldman <[hidden email]> wrote:

> > Date: Tue, 19 Jul 2005 04:30:03 -0700 (PDT)
> > From: kaustubh mendki <[hidden email]>
> >
> > I am trying a simple program to get MD5 hash of
> > any string.The program is as follows:
> >
> > #include <stdio.h>
> > #include "openssl/md5.h"
> >
> > int main()
> > {
> >         unsigned char var[]="k.";
> >         unsigned char *op;
> >      
> >         op = MD5(var,strlen(var),NULL);
> >         printf("\nHash= %s\tLen= %d",op,
> strlen(op));
> >         return 0;
> > }
> >
> > I compiled it with: gcc -lcrypto -o try try.c
> > The above code is returning empty hash with 0
> length.
> > But with strings like ".", "k.a" this code is
> > working properly.
> >
> > Can anyone throw light on it?
>
> Hashes are not printable C strings.  They are byte
> arrays.  Thus,
> printf and strlen will both fail.
>
> --
> Ken Goldman   [hidden email]   914-784-7646
>



       
               
__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

RE: MD5 not returning hash for string ending with "."

milan tomic-3

You are again using strlen():

hexprint(op, strlen(op));



> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of kaustubh mendki
> Sent: Tuesday, July 19, 2005 3:03 PM
> To: Ken Goldman
> Cc: [hidden email]
> Subject: Re: MD5 not returning hash for string ending with "."
>
>
> Hi Ken,
>
> Yes, hash can contain printable as well as
> nonprintable chars.As I mentioned this code
> was working with other strings and giving
> correct hash length(16).To eliminate this
> issue, i converted these chars to hex.
> Now code look like this:
>
> #include <stdio.h>
> #include "openssl/md5.h"
>
> void hexprint(unsigned char *data, int len)
> {    
>         int i;
>         for(i = 0;i < len;i++)
>         {    /* "%02x" for lowercase, "%02X" for
> uppercase hexidecimal letters */
>                 printf("%02x", data[i]);
>         }
> }
>  
> int main()
> {
>          unsigned char var[]="k.";
>          unsigned char *op;
>        
>          op = MD5(var,strlen(var),NULL);
>          hexprint(op, strlen(op));
>          return 0;        
> }
>
> Atleast this should work ..but its not
> working either for string "k.". For any
> other string working perfectly.
>
> Wonder if its a bug!
>
> Thanks,
> Kaustubh
>
>
> --- Ken Goldman <[hidden email]> wrote:
>
> > > Date: Tue, 19 Jul 2005 04:30:03 -0700 (PDT)
> > > From: kaustubh mendki <[hidden email]>
> > >
> > > I am trying a simple program to get MD5 hash of
> > > any string.The program is as follows:
> > >
> > > #include <stdio.h>
> > > #include "openssl/md5.h"
> > >
> > > int main()
> > > {
> > >         unsigned char var[]="k.";
> > >         unsigned char *op;
> > >      
> > >         op = MD5(var,strlen(var),NULL);
> > >         printf("\nHash= %s\tLen= %d",op,
> > strlen(op));
> > >         return 0;
> > > }
> > >
> > > I compiled it with: gcc -lcrypto -o try try.c
> > > The above code is returning empty hash with 0
> > length.
> > > But with strings like ".", "k.a" this code is
> > > working properly.
> > >
> > > Can anyone throw light on it?
> >
> > Hashes are not printable C strings.  They are byte
> > arrays.  Thus,
> > printf and strlen will both fail.
> >
> > --
> > Ken Goldman   [hidden email]   914-784-7646
> >
>
>
>
>
>
> __________________________________
> Do you Yahoo!?
> Yahoo! Mail - You care about security. So do we.
> http://promotions.yahoo.com/new_mail
> ______________________________________________________________________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    [hidden email]
> Automated List Manager                           [hidden email]
>

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]