Hash functions aren't very useful unless there's a way to give them an input value. In our GoLang implementation which is a command-line-interface (CLI) program, we can simply take in some input as a string and then represent that input as an array of bytes:
For example, if our input is the string "abc":
message := []byte("abc")
2 3fmt.Printf("Output: %b", message) // the %b prints the output in binary form 4
//Output: [1100001 1100010 1100011]
Message Block Creation
Once we have our input, the next step is to turn it into a message block or a series of message blocks that consists of an array of bits that's 448 congruent to 512bits in length.
All this means is we need to pad the bits from our message with enough bits to get it to a length that's 64 bits short of a multiple of 512.
To do this, we first add a 1 bit to denote where the padding bits start within the block, then we add zeros until we reach a bit length that's 64 bits short of a multiple of 512 bits.
First, we take our message bytes as input via the function BuildMessageBlocks.
We then create another byte array which we can build upon and copy the bytes from the input message so we can pass our input message bytes to subsequent functions.
The first two steps of BuildMessageBlocks result in the following output:
The next step adds the required padding to get to the 448 bits congruent to 512-bit length.
Since there are 8 bits in a byte, it follows that there are 512 bits in 64 bytes and 448 bits in 56 bytes. Therefore, if our message length in bytes modulous 64 (i.e. the remainder of our message length divided by 64 bytes) is less than 56, it means our message length is small enough to fit within one 64-byte block. Otherwise, we need to add padding to create more than one message block.
Once we've determined the amount of padding that needs to be added, we add our separator bit using the hexadecimal representation of 128 to add the 1 bit to the front of the byte immediately following our message bytes, and then we add our zero bits to the amount we determined in the previous step:
func howMuchPadding(message []byte) int {
messageLength := len(message)
result := 0
if messageLength%64 < 56 {
result = 56 - messageLength%64
} else {
result = 64 + 56 - messageLength%64
}
return result
}
func addZeroPaddingBits(messageBlock []byte, message []byte) []byte {
zeros := make([]byte, howMuchPadding(message))
//add separator bit
zeros[0] += 0x80
return append(messageBlock, zeros...)
}
Our resulting output is our message with our required padding:
The final step in our message block(s) construction is to add the length of our input message using the last 64 bits or 8 bytes of our message block space to get our message block to a multiple of 512 bits or 64 bytes.
To do this, we calculate the length of our message in bytes as a 64 bit unsigned integer value, and then we append this value in bytes to the end of our message block as an unsigned 64 bit integer to match our required bit length. In our case, the length of our input message "abc" is 24 bits or 3 bytes:
Now that we have our message block(s), we can proceed to creating our message schedules.
Message schedules in the SHA-256 hash function consist of 64 32 bit 'words' which can be represented as unsigned 32 bit integers.
Each 512 bit message block is used to create a 64 word message schedule. The first 16 words consist of the message block itself: 512/32 = 16.
The last 48 words are computed from the first 16 words using logical functions. You should also be aware that each sum in SHA-256 is computed modulo 232 meaning the result is always constrained to 32 bits. Since we're using uint32 variables to hold our words, we don't need to worry about this as it will be done automatically for us.
BuildMessageSchedule takes a messageBlock as input and uses it to build and return a message schedule:
package preprocessing
import "encoding/binary"
import "sha256-example/logicalFunctions"
func BuildMessageSchedule(message []byte) [64]uint32 {
schedule := getFirst16Words(BuildMessageBlocks(message))
schedule = getRemaining48Words(schedule)
return schedule
}
func getFirst16Words(messageBlock []byte) [64]uint32 {
var schedule [64]uint32
j := 0
for i := 0; i < 64; i += 4 {
schedule[j] = binary.BigEndian.Uint32(messageBlock[i:])
j++
}
return schedule
}
func getRemaining48Words(schedule [64]uint32) [64]uint32 {
for i := 16; i < 64; i++ {
schedule[i] = bitwiseMovements.SmallSigmaOne(schedule[i-2]) + schedule[i-7] + bitwiseMovements.SmallSigmaZero(schedule[i-15]) + schedule[i-16]
}
return schedule
}
The first step is to parse the input message block into 16 32 bit words and add them to our message schedule which is represented as an array of 64 uint32 values:
Where ROTR indicates a bit rotation to the right which wraps bits from the right side of the 32-bit array around to the left side; SHR represents a bit shift to the right which does not wrap bits, and XOR represents an exclusive or logical operation where only one of two bits being added together is a 1 for a result of 1.
Now that we have our message schedule, we can move on to the hash computation or compression function of the SHA-256 hash function.
Note: the message block creation step is performed once, and for each of the 512 bit sections of the message block, a message schedule is created and is compressed in the following steps with the result being passed between each step using 'chaining variables'.
\