【问题标题】:How do I calculate SHA-256 hash of a list in Python?如何在 Python 中计算列表的 SHA-256 哈希?
【发布时间】:2021-04-18 06:56:13
【问题描述】:

我正在尝试复制 JS 方法:

JS代码:


const toHexCopy = (buffer) => {
            return [...new Uint8Array (buffer)]
                .map (b => b.toString (16).padStart (2, "0"))
                .join ("");
};

ar = new Uint8Array([0, 1, 2]);
hash = await crypto.subtle.digest('SHA-256', ar)
console.log(toHexCopy(hash))

// returns 039058c6f2c0cb492c533b0a4d14ef77cc0f78abccced5287d84a1a2011cfb81

如何在 Python 中复制它?

【问题讨论】:

  • 您要散列list 还是字节序列?这些是非常不同的问题。
  • @Brian,基本上我想获得与 JS 相同的输出。我在 Python 中有一个列表,如果需要,我可以将其转换为字节。

标签: python-3.x list sha256


【解决方案1】:
from hashlib import sha256
print(sha256(bytes([1, 2, 3])).hexdigest())

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 2012-06-15
    • 2011-11-02
    • 2019-07-21
    • 2014-08-19
    • 1970-01-01
    • 2010-12-17
    • 2015-03-15
    • 2019-04-01
    相关资源
    最近更新 更多