Why does the hash from using openssl differ from the ones I get in python?
为什么使用openssl的散列与我在python中使用的散列不同?
$ echo "Lorem ipsum" | openssl dgst -sha1 -hex
(stdin)= d0c05753484098c61e86f402a2875e68992b5ca3
$ python
>>> from hashlib import sha1
>>> sha("Lorem ipsum").hexdigest()
'94912be8b3fb47d4161ea50e5948c6296af6ca05'
>>> from Crypto.Hash import SHA
>>> SHA.new("Lorem ipsum").hexdigest()
'94912be8b3fb47d4161ea50e5948c6296af6ca05'
Are the strings not equivalent? am I missing something obvious?
字符串不相等吗?我是不是漏掉了什么明显的东西?
Edit: Thanks for spotting it. Was piping a saved message from a file which also suffer from the same annoying newline issue.
编辑:谢谢你的发现。从一个文件中发送一个保存的消息,该文件也遭受同样恼人的换行问题。
$ cat message | openssl dgst -sha1 -hex
'keep whacking your head mate, it wont be the same'
$ echo -n $(cat message) | openssl dgst -sha1 -hex
'ok, you got me, for now'
3 个解决方案
#1
24
You're missing the endline that echo
will append by default:
你错过了echo在默认情况下附加的endline:
echo "Lorem ipsum" | openssl dgst -sha1 -hex
(stdin)= d0c05753484098c61e86f402a2875e68992b5ca3
With the -n
parameter, it will echo only the string that you gave it, for expected result:
对于-n参数,它只会返回你给它的字符串,用于预期的结果:
echo -n "Lorem ipsum" | openssl dgst -sha1 -hex
(stdin)= 94912be8b3fb47d4161ea50e5948c6296af6ca05
#2
6
echo is putting a newline at the end of the string
echo在字符串的末尾添加一个换行符。
>>> sha("Lorem ipsum\n").hexdigest()
'd0c05753484098c61e86f402a2875e68992b5ca3'
#3
1
echo
adds a newline character to the string. The option -n suppresses the tailing newline:
echo将换行符添加到字符串中。选项-n抑制尾线:
> echo -n "Lorem ipsum" | openssl dgst -sha1 -hex
94912be8b3fb47d4161ea50e5948c6296af6ca05
#1
24
You're missing the endline that echo
will append by default:
你错过了echo在默认情况下附加的endline:
echo "Lorem ipsum" | openssl dgst -sha1 -hex
(stdin)= d0c05753484098c61e86f402a2875e68992b5ca3
With the -n
parameter, it will echo only the string that you gave it, for expected result:
对于-n参数,它只会返回你给它的字符串,用于预期的结果:
echo -n "Lorem ipsum" | openssl dgst -sha1 -hex
(stdin)= 94912be8b3fb47d4161ea50e5948c6296af6ca05
#2
6
echo is putting a newline at the end of the string
echo在字符串的末尾添加一个换行符。
>>> sha("Lorem ipsum\n").hexdigest()
'd0c05753484098c61e86f402a2875e68992b5ca3'
#3
1
echo
adds a newline character to the string. The option -n suppresses the tailing newline:
echo将换行符添加到字符串中。选项-n抑制尾线:
> echo -n "Lorem ipsum" | openssl dgst -sha1 -hex
94912be8b3fb47d4161ea50e5948c6296af6ca05