0

I am trying to use struct.pack to pack a hash disgest, but not getting the expected result.

This is how I am packing the data:

hash = hashlib.sha256(input).digest()
print('hash = ', hash.hex())
packed = struct.pack('!32p', hash)
print('packed = ', packed.hex()) 

Here is an example result: hash = b5dbdb2b0a7d762fc7e429062d64b711d240e8f95f1c59fc28c28ac6677ffeaf

packed = 1fb5dbdb2b0a7d762fc7e429062d64b711d240e8f95f1c59fc28c28ac6677ffe

The bytes appear to be shifted, and "1f" has been added. Is this a result of an incorrect format specifier?

EDIT: I believe this first byte is the length of the data, because I am using 'p'. Is there any way to avoid this? I don't want to include this in my packed data

user9241855
  • 25
  • 1
  • 7

1 Answers1

0

The 'p' format character encodes a “Pascal string” which includes the string's length at the beginning. This is documented. If you don't want that use 's' format to get just the bytes themselves instead:

packed = struct.pack('!32s', hash)
print('packed =', packed.hex())

Output:

packed = b5dbdb2b0a7d762fc7e429062d64b711d240e8f95f1c59fc28c28ac6677ffeaf
martineau
  • 119,623
  • 25
  • 170
  • 301