# What is the purpose of placing the most significant byte first?

When the SHA algorithm states "A block of 128 bits is appended to the message. This block is treated as an unsigned 128-bit integer (most significant byte first) [emphasis added] and contains the length of the original message (before the padding)[,]" what is the purpose of placing the most significant byte first?

For example, if we have an integer of 24, which would be 00011000, and we therefore need 15 bytes of 0's to complete our 128-bit block, does this mean that 00011000 comes first followed by 15 bytes of 0's? It seems like this must be what they're saying.

I'm just confused why this byte needs to be first...

Source: Network Security Essentials (6th ed), William Stallings, p. 72

## 2 Answers

The purpose of specifying this is that some machines are big-endian, and some are little-endian. In other words, some computers internally represent integers such that the most-significant digits come first, as our human numbering systems do, while other computer architectures natively store the least-significant bytes first. Leaving this unspecified would result in these two types of computers arriving at very different answers, so the SHA algorithm explicitly specifies the memory layout of the integers it uses to avoid ambiguity.

In your example, the 15 bytes of zeroes would be on the left, as those bytes are most significant (in decimal, it's the equivalent of writing 0024 instead of 24). Were they to specify little-endian order, the 0001100 would come first, as those are the least-significant bytes (again for decimal, it would be like writing 4200, which is still interpreted as the decimal value 24, just represented differently).

$24$ expressed as a 16-byte hexadecimal number is $$00\;00\;00\;00\;00\;00\;00\;00\;00\;00\;00\;00\;00\;00\;00\;18$$ The most significant byte is the leftmost. $00011000_2$ comes last, not first.

It makes no difference to the security of the algorithm whether the most significant byte comes first or last. What is important is that everybody agrees on this point.