How to get the sha256 of a 'binary' array in Python

942 Views Asked by At

given the following Python code:

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)

how can I get the binary string of the sha256 of the bits/ binstr. (Meaning how to achieve binary(sha256(bits)). I got stuck when using something like:

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

Maybe you could help me to find my mistakes.

1

There are 1 best solutions below

1
On

There's probably a library function to do it but you can convert the hex string to an int, then the int to a bin.

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