tags:

views:

176

answers:

4

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
+4  A: 

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

GregS
I have tried with only echo abc | md5but still get the incorrect hash.How do I remove the linefeed or do you know of another way of creating md5 hashes quickly using ksh? 0bee89b07a248e27c83fc3d5951213c1
Anthony
@Anthony: try `/bin/echo -n abc | md5` or maybe /usr/bin/echo
ysth
+4  A: 

use the more portable printf

printf "abc" | md5
ghostdog74
Thank you very much. Don't know why I didn't post the question here earlier.
Anthony
After seeing these responses and thanks to @ghostdog74 and Dennis Wiliamson I ended up going with echo "abc/c" | md5I reckon I read that man page 15 times but never noticed the bit about /c.
Anthony
+1  A: 

Further to GregS's answer, some examples:

$ md5 -s "-n abc"$'\012' # \012 = newline
MD5 ("-n abc
") = 62fecf21103616856d72e6ffc9bcb06b

And

$ md5 -s "abc"
MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
martin clayton
A: 

When I run into these issues, I normally do something like this to check what my output is (for example showing tabs, versus spaces and so forth)

$ echo abc | hexdump -c
0000000   a   b   c  \n                                                
0000004
hpavc

related questions