为什么Python创build的MD5散列与使用shell中的echo和md5sum创build的散列不同?

Python MD5散列与shell中md5sum命令创build的散列不同。 为什么?

>>> import hashlib >>> h = hashlib.md5() >>> h.update("mystringforhash") >>> print h.hexdigest() 86b6423cb6d211734fc7d81bbc5e11d3 # Result from Python $ echo mystringforhash | md5sum 686687dd68c5de717b34569dbfb8d3c3 - # Result on the shell 

echo附加一个\n因为你通常不需要在你的shell中没有以换行符结尾的行(如果提示没有从最左边开始,它看起来真的很难看)。
使用-n参数来省略尾随换行符,它将打印与您的Python脚本相同的校验和:

 > echo -n mystringforhash | md5sum 86b6423cb6d211734fc7d81bbc5e11d3 -