Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python openssl和hashlib/pycrypto之间的SHA1哈希不同_Python_Openssl_Pycrypto_Hashlib - Fatal编程技术网

Python openssl和hashlib/pycrypto之间的SHA1哈希不同

Python openssl和hashlib/pycrypto之间的SHA1哈希不同,python,openssl,pycrypto,hashlib,Python,Openssl,Pycrypto,Hashlib,为什么使用openssl的散列与我在python中得到的散列不同 $ echo "Lorem ipsum" | openssl dgst -sha1 -hex (stdin)= d0c05753484098c61e86f402a2875e68992b5ca3 $ python >>> from hashlib import sha1 >>> sha("Lorem ipsum").hexdigest() '94912be8b3fb47d4161ea50e5948

为什么使用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'
字符串不是等价的吗?我错过了什么明显的东西吗

编辑:感谢您发现它。正在从一个文件中传送一条保存的消息,该文件也有同样恼人的换行符问题

$ 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' 

echo在字符串末尾加了一个换行符

>>> sha("Lorem ipsum\n").hexdigest()
'd0c05753484098c61e86f402a2875e68992b5ca3'

您缺少默认情况下
echo
将附加的结束行:

echo "Lorem ipsum" | openssl dgst -sha1 -hex
(stdin)= d0c05753484098c61e86f402a2875e68992b5ca3
使用
-n
参数,它将只回显您提供给它的字符串,以获得预期结果:

echo -n "Lorem ipsum" | openssl dgst -sha1 -hex
(stdin)= 94912be8b3fb47d4161ea50e5948c6296af6ca05

echo
在字符串中添加换行符。选项-n不显示尾随换行符:

> echo -n "Lorem ipsum" | openssl dgst -sha1 -hex
94912be8b3fb47d4161ea50e5948c6296af6ca05

普通的,是的,明显的,不是真的。