# SHA 256

#### Wictorian

Hello. I have been coding the SHA-256 Algorithm in python. I have to store the length of the input in big endian form in 48 bits. For instance, when the input is 88 bits long, it looks like this:
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 01011000
The question is, how am I supposed to implement this? I understood how however what to do when the input is longer than 256 bits long?

#### Kuncode

Hello. I have been coding the SHA-256 Algorithm in python. I have to store the length of the input in big endian form in 48 bits. For instance, when the input is 88 bits long, it looks like this:

The question is, how am I supposed to implement this? I understood how however what to do when the input is longer than 256 bits long?
To store the length of the input in big endian form in 48 bits, you can use the struct library in Python. It provides functionality to pack values into binary data represented as bytes objects.

Here's an example of how you can pack the input length into 48 bits in big endian form:

Python:
``````import struct

def get_input_bits_len(input_data):
input_len_bits = len(input_data) * 8
packed = struct.pack(">Q", input_len_bits)
return packed[:-6]``````

When the input is longer than 256 bits, you will have to process the input in blocks of 256 bits and store the length of each block in the corresponding way. After processing all the blocks, you can concatenate the outputs to get the final output.

#### Wictorian

To store the length of the input in big endian form in 48 bits, you can use the struct library in Python. It provides functionality to pack values into binary data represented as bytes objects.

Here's an example of how you can pack the input length into 48 bits in big endian form:

Python:
``````import struct

def get_input_bits_len(input_data):
input_len_bits = len(input_data) * 8
packed = struct.pack(">Q", input_len_bits)
return packed[:-6]``````

When the input is longer than 256 bits, you will have to process the input in blocks of 256 bits and store the length of each block in the corresponding way. After processing all the blocks, you can concatenate the outputs to get the final output
Thanks but I'd rather do this without using libraries because what I need in the end is a string I suppose. So I have to store the endian representation like this:
Code:
``````"""00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 01011000"""``````

#### WhiteCube

Hello. I have been coding the SHA-256 Algorithm in python. I have to store the length of the input in big endian form in 48 bits. For instance, when the input is 88 bits long, it looks like this:

The question is, how am I supposed to implement this? I understood how however what to do when the input is longer than 256 bits long?
The example is easily misunderstood, why are there 52 bytes?

#### Wictorian

The example is easily misunderstood, why are there 52 bytes?
Which 52 bytes?

#### fluffycorleone

8 bits = 01010101
8 bits = 1 byte

You have way more than 88 bits in OP

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

### Members online

No members online now.