【问题标题】:What is the difference between md5sum output and Python hashlib output?md5sum 输出和 Python hashlib 输出有什么区别?
【发布时间】:2018-05-25 16:11:25
【问题描述】:

为什么hashlib.md5().hexdigest() 的输出与md5sumopenssl 的输出不同?

$ echo "test string" | md5sum
f299060e0383392ebeac64b714eca7e3  -
$ echo "test string" | openssl dgst -md5
(stdin)= f299060e0383392ebeac64b714eca7e3
$ python
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from hashlib import md5
>>> print(md5("test string").hexdigest())
6f8db599de986fab7a21625b7916589c

我在尝试生成用于 Gravatar 的 md5 摘要时注意到了这一点。 Python hashlib 输出有效,但 md5sumopenssl 输出无效。

【问题讨论】:

    标签: python bash md5 gravatar


    【解决方案1】:

    echo 默认添加一个隐式换行符。

    $ echo -n "test string" | openssl dgst -md5
    (stdin)= 6f8db599de986fab7a21625b7916589c
    

    【讨论】:

    • 知道我错过了一些东西。谢谢!我会尽快接受。
    • printf '%s' "test string" 如果您使用的是不支持-n 的shell 或echo
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    • 2020-12-02
    • 2019-06-24
    • 2020-04-27
    • 2017-03-17
    • 2017-03-16
    相关资源
    最近更新 更多