Why doesn"t Digest::MD5::md5_hex match GNU md5sum?

Why doesn"t Digest::MD5::md5_hex match GNU md5sum?

am 21.11.2007 23:18:56 von David Filmer

Greetings. Kindly consider these two methods to create an MD5 digest
(in hex format):

perl -e "use Digest::MD5 'md5_hex'; print md5_hex('foo')"
acbd18db4cc2f85cedef654fccc4a4d8

echo foo |md5sum
d3b07384d113edec49eaa6238ad5ff00

Ummm, shouldn't those be the same?

perldoc Digest::MD5
[snip]
The MD5 algorithm is defined in RFC 1321

md5sum --help
md5sum (GNU coreutils) 6.4
[snip]
The sums are computed as described in RFC 1321.

Can someone tell me why these two methods produce different output?

Thanks!

--
David Filmer (http://DavidFilmer.com)

Re: Why doesn"t Digest::MD5::md5_hex match GNU md5sum?

am 21.11.2007 23:25:49 von Eric Schwartz

David Filmer writes:
> Greetings. Kindly consider these two methods to create an MD5 digest
> (in hex format):
>
> perl -e "use Digest::MD5 'md5_hex'; print md5_hex('foo')"
> acbd18db4cc2f85cedef654fccc4a4d8
>
> echo foo |md5sum
> d3b07384d113edec49eaa6238ad5ff00
>
> Ummm, shouldn't those be the same?

No.

$ echo foo | od -c
0000000 f o o \n
0000004

You're getting an extra newline there. If you want to compare them,
try passing echo the -n flag, which tells it not to append a newline
to its parameters:

$ echo -n foo | md5sum
acbd18db4cc2f85cedef654fccc4a4d8 -

> Can someone tell me why these two methods produce different output?

Because you're passing them different input. You just didn't notice
it.

-=Eric

Re: Why doesn"t Digest::MD5::md5_hex match GNU md5sum?

am 21.11.2007 23:26:47 von Martijn Lievaart

On Wed, 21 Nov 2007 14:18:56 -0800, David Filmer wrote:

> Greetings. Kindly consider these two methods to create an MD5 digest
> (in hex format):
>
> perl -e "use Digest::MD5 'md5_hex'; print md5_hex('foo')"
> acbd18db4cc2f85cedef654fccc4a4d8
>
> echo foo |md5sum
> d3b07384d113edec49eaa6238ad5ff00
>
> Ummm, shouldn't those be the same?

No, the input is different, so the output should be different as well.

Try:
perl -e 'use Digest::MD5 "md5_hex"; print md5_hex("foo\n")'

HTH,
M4