4

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

2 Answers 2

4

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()
Sign up to request clarification or add additional context in comments.

Comments

3

You are reusing the hash object which already contains bits from rawhex4. If you create a new one, you'll get your 26..fa result.

2 Comments

Good explanation. Steal my code for an example if you feel the need; I'll delete my answer in a little while.
Wow I thought with h.update() the hash function was cleared so I thought my problem was in concatenating bytes... couldn't see the simple solution... Thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.