English 中文(简体)
Why does Perl and /bin/sha1 give different results?
原标题:
  • 时间:2009-12-03 22:20:40
  •  标签:
  • perl
  • sha1

I m confused as to why the following return separate sHA1s

$ perl -MDigest::SHA1 -E say Digest::SHA1::sha1_hex("http://i.aultec.com/v/8066/Originals/1FTVX12585NA9832010.jpg"); 
e1133fa3b7ea0bfb8ffa4d877932ed6c6fa10cef

$ echo "http://i.aultec.com/v/8066/Originals/1FTVX12585NA9832010.jpg" | sha1sum 
5c3731e83ae0184ed93b595b9f5604863dd331e6  -

Which one is right? Am /I/ doing it wrong?

$ perl -MDigest::SHA -E say Digest::SHA::sha1_hex("http://i.aultec.com/v/8066/Originals/1FTVX12585NA9832010.jpg"); 
e1133fa3b7ea0bfb8ffa4d877932ed6c6fa10cef

You can see the digest is right in the successor (Digest::SHA)

最佳回答

Both are right. Your echo command includes a newline at the end. (and the perl string doesn t) Try with echo -n ...

问题回答

Perl is giving you the hash of the literal string you entered, whereas echo is appending a newline. If you tell echo to not add a newline, you ll get the same result:

drewfus:~$ perl -MDigest::SHA1 -E say Digest::SHA1::sha1_hex("foo"); 
0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
drewfus:~$ echo -n "foo" | sha1sum
0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33  -

This is such a frequent mistake and I ve made it many times. The echo command is also returning a newline.





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签