Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with bytes in Python using SHA-256

So I want to use SHA-256 for a specific problem: Calculate digest from a bytearray, then concatenate the resulting digest to another block of bytes ( a 1024 byte block for this problem ) and calculate the digest for the concatenated values.

For example:

Here are my two byte blocks:

from hashlib import sha256
rawhex4 = b'\x44'*773
rawhex3 = b'\x33'*1024

h = sha256()
h.update(rawhex4)
aux = h.digest()

This hexdigest is: d8f8a9eadd284c4dbd94af448fefb24940251e75ca2943df31f7cfbb6a4f97ed

then I want to concatenate this 32 byte digest to my next block and hash it but I am not getting the correct answer. I do the following:

h.update(rawhex3 + aux)

I know for fact that hashing rawhex3 + hash(rawhex4) will give me this digest:

26949e3320c315f179e2dfc95a4158dcf9a9f6ebf3dfc69252cd83ad274eeafa

What could I be missing? I am pretty new to Python

like image 721
Jesus Fernandez Avatar asked Mar 13 '23 16:03

Jesus Fernandez


1 Answers

Try this:

from hashlib import sha256
rawhex4 = b'\x44'*773
rawhex3 = b'\x33'*1024

h1 = sha256()
h1.update(rawhex4)
aux = h1.digest()

h2 = sha256()
h2.update(rawhex3 + aux)
print h2.hexdigest()
like image 172
Robᵩ Avatar answered Mar 16 '23 06:03

Robᵩ