2

Trying to recreate SHA256 hashing from the pseudocode on the wiki and it says to convert an integer to a 64 bit big endian integer.

I can't find how to do this anywhere online and when I look up the step by step process of how SHA256 works it doesn't make sense to me when it gets to that part as it seems they are just using a 7 bit unsigned binary integer in their example: https://qvault.io/cryptography/how-sha-2-works-step-by-step-sha-256/

can anyone help?

3 Answers 3

3

Ints in Python 3 have a method called to_bytes() with arguments for length in bytes and endianness which returns a bytes object.

>>> num = 5
>>> print(num.to_bytes(8, 'big')) # 8 bytes aka 64 bits long, big endian
b'\x00\x00\x00\x00\x00\x00\x00\x05'
Sign up to request clarification or add additional context in comments.

Comments

2

You certain can do this with struct:

>>> import struct
>>> val = 1234
>>> struct.pack('>Q',val)
b'\x00\x00\x00\x00\x00\x00\x04\xd2'

Whether that's the best thing to use for SHA256 is a question for the philosophers.

Comments

0

it seems they are just using a 7 bit unsigned binary integer in their example: https://qvault.io/cryptography/how-sha-2-works-step-by-step-sha-256/

In that example the integer 88 has a bit length of 7 (1011000) however if you look closely at the pre-processing steps you'll see it is appended to the data as 64 bits:

00000000 00000000 00000000 00000000 00000000 00000000 00000000 01011000

One way to get the 64-bit integer for length is format(L,"064b"), where L is the bit length of the original message. For a bit length of 88 this would return 0000000000000000000000000000000000000000000000000000000001011000.

Comments

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.