【问题标题】:Bash sha256 not matching erlang one [duplicate]Bash sha256不匹配二郎一[重复]
【发布时间】:2021-11-15 06:10:21
【问题描述】:

当我“sha256”时,Erlang 返回的结果与 bash 命令不同。

echo a | sha256sum, returns: 87428fc522803d31065e7bce3cf03fe475096631e5e07bbd7a0fde60c4cf25c7  -

二郎

    Bin = crypto:hash(sha256, "a").


<<202,151,129,18,202,27,189,202,250,194,49,179,154,35,220,
      77,167,134,239,248,20,124,78,114,185,128,119,133,175,
      238,72,187>>

我尝试了从 bin to hex 到十六进制的不同 bin 他们都没有给出我期望的结果。

我得到了这个结果:

    bin_to_hex:bin_to_hex(Bin).
<<"CA978112CA1BBDCAFAC231B39A23DC4DA786EFF8147C4E72B9807785AFEE48BB">>

【问题讨论】:

    标签: cryptography erlang elixir sha256 erlang-otp


    【解决方案1】:

    您的echo a 将包含一个换行符。当我在 erlang 版本中添加换行符时,我得到了预期的哈希:

    1> Bin = crypto:hash(sha256, "a\n").
    <<135,66,143,197,34,128,61,49,6,94,123,206,60,240,63,228,
      117,9,102,49,229,224,123,189,122,15,222,96,196,...>>
    2> binary:encode_hex(Bin).
    <<"87428FC522803D31065E7BCE3CF03FE475096631E5E07BBD7A0FDE60C4CF25C7">>
    

    您也可以告诉echo 不要在-n 中使用换行符:

    $ echo -n a | sha256sum
    ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb  -
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-26
      • 1970-01-01
      • 1970-01-01
      • 2015-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多