首页 > 解决方案 > 如何在 Python 中计算列表的 SHA-256 哈希?

问题描述

我正在尝试复制 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 中复制它?

标签: python-3.xlistsha256

解决方案


from hashlib import sha256
print(sha256(bytes([1, 2, 3])).hexdigest())

推荐阅读