【问题标题】:Discrepancy between sha256sum executable and PyCrypto librarysha256sum 可执行文件和 PyCrypto 库之间的差异
【发布时间】:2012-12-07 14:42:35
【问题描述】:

我正在尝试获取 ASCII 编码字符串的 SHA256 和。首先,我尝试了sha256sum 可执行文件:

$ echo foo | sha256sum
b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c

但是当我使用 PyCrypto 库时,我得到了一些不同的东西:

from Crypto.Hash import SHA256
h = SHA256.new();
h.update('foo');
print(h.hexdigest());

我得到以下信息:

c5aac592460a9ac7845e341090f6f9c81f201b63e5338ee8948a6fe6830c55dc

我怀疑我遗漏了第一个的某些内容,即echo foo 可能有分隔符或其他内容,但我无法弄清楚是什么。

这两种情况有什么不同?

【问题讨论】:

    标签: python command-line sha256


    【解决方案1】:

    命令echo foo 在输出末尾添加一个换行符,您应该使用-n 选项:

    $ echo -n foo | sha256sum
    2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae  -
    $ python
    Python 2.7.3 (default, Sep 26 2012, 21:53:58) 
    [GCC 4.7.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import hashlib
    >>> hashlib.sha256('foo').hexdigest()
    '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'
    

    【讨论】:

      猜你喜欢
      • 2021-07-04
      • 1970-01-01
      • 2011-08-19
      • 2011-02-10
      • 2014-10-19
      • 1970-01-01
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多