From: pwu on
Hi,

I am generating a MD5 checksum as follows:
MD5Init(&md5handle);

in=open("myfile",O_RDONLY);
while (count=read(in,buf,sizeof(buf))>0) {
MD5Update(&md5handle,buf,count);
}
close(in);

MD5Final(buf,&md5handle);
for (i=0;i<16;++i) printf("%02x",(unsigned char) buf[i]);
printf("\n");

This generates:
32ccd14017f783d031a3be893f18a9d7

However if I:
MD5(myfile)= 53c6a47024849e652006826a39144892

Can someone explain why the checksums differ? They are calculated from
the same file.

Thanks
From: Joshua Maurice on
On Jul 8, 7:25 pm, p...(a)qantas.com.au wrote:
> Hi,
>
> I am generating a MD5 checksum as follows:
> MD5Init(&md5handle);
>
>   in=open("myfile",O_RDONLY);
>   while (count=read(in,buf,sizeof(buf))>0) {
>     MD5Update(&md5handle,buf,count);
>   }
>   close(in);
>
>    MD5Final(buf,&md5handle);
>    for (i=0;i<16;++i) printf("%02x",(unsigned char) buf[i]);
>   printf("\n");
>
> This generates:
> 32ccd14017f783d031a3be893f18a9d7
>
> However if I:
> MD5(myfile)= 53c6a47024849e652006826a39144892
>
> Can someone explain why the checksums differ? They are calculated from
> the same file.

First, you might want to post complete examples so the rest of us can
follow what you're doing. For example, what is MD5Final? What is
MD5(...)? Is that a command executed in a shell?

Also, you want may to open the file in binary mode. That might be your
problem.
From: Ben Bacarisse on
pwu(a)qantas.com.au writes:

> I am generating a MD5 checksum as follows:
> MD5Init(&md5handle);
>
> in=open("myfile",O_RDONLY);
> while (count=read(in,buf,sizeof(buf))>0) {

You want:

while ((count = read(in, buf, sizeof buf)) > 0)

I've written that how I would: more spaces and with no ()s round
sizeof's object operand. The key point being that you were assigning 0
or 1 to count, not the result of the read.

Turn up your compiler's warning level and it might catch this sort of
thing automatically.

BTW, why use open when you link stdio (for printf) anyway?

> MD5Update(&md5handle,buf,count);
> }
> close(in);
>
> MD5Final(buf,&md5handle);
> for (i=0;i<16;++i) printf("%02x",(unsigned char) buf[i]);
> printf("\n");

<snip>
--
Ben.