SHA 256

Joined
Feb 6, 2023
Messages
46
Reaction score
3
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?
 
Joined
Jan 30, 2023
Messages
105
Reaction score
12
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.
 
Joined
Feb 6, 2023
Messages
46
Reaction score
3
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"""
 
Joined
Sep 20, 2022
Messages
237
Reaction score
39
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

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.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,146
Members
48,775
Latest member
satman49

Latest Threads

Top