【问题标题】:How to get the sha256 of a 'binary' array in Python如何在 Python 中获取“二进制”数组的 sha256
【发布时间】:2021-05-18 21:12:31
【问题描述】:

给定以下 Python 代码:

bits = [0]*256  # for simplicity (actually it's more like (0|1)^n)
binstr = ""  # make string containing all 'bits'
for el in bits:
    binstr += str(el)

如何获取 bits/binstr 的 sha256 的二进制字符串。 (意思是如何实现二进制(sha256(位))。 我在使用类似的东西时卡住了:

import hashlib
import binascii

hexstr = "{0:0>4X}".format(int(binstr, 2))
data = binascii.a2b_hex(hexstr)
print(data)
> b'\x00\x00'
output = hashlib.sha256(data).hexdigest()
print(output)
> 96a296d224f285c67bee93c30f8a309157f0daa35dc5b87e410b78630a09cfc7

也许你可以帮我找出我的错误。

【问题讨论】:

  • 运行代码时会发生什么?
  • @itprorh66 我添加打印结果

标签: python python-3.x sha256 hashlib binascii


【解决方案1】:

可能有一个库函数可以执行此操作,但您可以将十六进制字符串转换为 int,然后将 int 转换为 bin。

bin(int(hashlib.sha256("whatever you want to hash").hexdigest(), 16))

【讨论】:

  • 当然,但是这个“你想要散列的任何东西”是一个字符串,我只有一个位数组(如果需要,我可以连接到一个位串)。所以我希望哈希不要使用字符串,比如 sha("01010") 我想要类似 sha(01010) 的东西。
猜你喜欢
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 2014-07-19
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多