Why does Perl and /bin/sha1 give different results?

与世无争的帅哥 提交于 2019-12-01 17:50:42

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!