Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't md5 match Perl's Digest::MD5 output?

Tags:

ksh

md5

perl

Running the md5 function from the ksh terminal does not matching the output from a simple Perl script.

In the terminal I run:

echo -n abc | md5
62fecf21103616856d72e6ffc9bcb06b

If I run it using Perl:

use Digest::MD5 qw(md5_hex);

foreach (@ARGV) {
   print "Digest is ", md5_hex($_), "\n";
}
exit

I get

./perl_test.sh abc
Digest is 900150983cd24fb0d6963f7d28e17f72

In all the samples I see and the sample of the md5 function itself the Perl one looks right but the one using just Ksh doesn't:

md5 -x
MD5 test suite:
MD5 ("") = d41d8cd98f00b204e9800998ecf8427e
MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661
MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
like image 438
Anthony Avatar asked Feb 16 '10 01:02

Anthony


2 Answers

use the more portable printf

printf "abc" | md5
like image 131
ghostdog74 Avatar answered Oct 18 '22 03:10

ghostdog74


Your echo doesn't recognize the -n option, so you are hashing the string '-n abc\n'.

like image 32
President James K. Polk Avatar answered Oct 18 '22 02:10

President James K. Polk