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

D

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!
 
E

Eric Schwartz

David Filmer said:
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
 
M

Martijn Lievaart

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top