1

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.

Root_DE
  • 71
  • 6

1 Answers1

0

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))
Daniel Williams
  • 8,673
  • 4
  • 36
  • 47
  • sure, but this "whatever you want to hash" is a string, I only have an array of bits (which I can concatenate to a string of bits if necessary). So I want the hash not to using the string, like sha("01010") i want something like sha(01010). – Root_DE May 19 '21 at 11:48