4. Message Padding and Parsing
The hash functions specified herein are used to compute a message digest for a message or data file that is provided as input. The message or data file should be considered to be a bit string. The length of the message is the number of bits in the message (the empty message has length 0). If the number of bits in a message is a multiple of 8, for compactness we can represent the message in hex. The purpose of message padding is to make the total length of a padded message a multiple of 512 for SHA-224 and SHA-256 or a multiple of 1024 for SHA-384 and SHA-512.
The following specifies how this padding shall be performed. As a summary, a "1" followed by m "0"s followed by a 64-bit or 128-bit integer are appended to the end of the message to produce a padded message of length 512n or 1024n. The appended integer is the length of the original message. The padded message is then processed by the hash function as n 512-bit or 1024-bit blocks.
4.1. SHA-224 and SHA-256
Suppose a message has length L < 2^64. Before it is input to the hash function, the message is padded on the right as follows:
a. "1" is appended. Example: if the original message is "01010000", this is padded to "010100001".
b. K "0"s are appended where K is the smallest, non-negative solution to the equation
( L + 1 + K ) mod 512 = 448
c. Then append the 64-bit block that is L in binary representation. After appending this block, the length of the message will be a multiple of 512 bits.
Example: Suppose the original message is the bit string
01100001 01100010 01100011 01100100 01100101
After step (a) this gives
01100001 01100010 01100011 01100100 01100101 1
Since L = 40, the number of bits in the above is 41 and K = 407 "0"s are appended, making the total now 448. This gives the following in hex:
61626364 65800000 00000000 00000000
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000
00000000 00000000
The 64-bit representation of L = 40 is hex 00000000 00000028. Hence the final padded message is the following hex:
61626364 65800000 00000000 00000000
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000028
4.2. SHA-384 and SHA-512
Suppose a message has length L < 2^128. Before it is input to the hash function, the message is padded on the right as follows:
a. "1" is appended. Example: if the original message is "01010000", this is padded to "010100001".
b. K "0"s are appended where K is the smallest, non-negative solution to the equation
( L + 1 + K ) mod 1024 = 896
c. Then append the 128-bit block that is L in binary representation. After appending this block, the length of the message will be a multiple of 1024 bits.
Example: Suppose the original message is the bit string
01100001 01100010 01100011 01100100 01100101
After step (a) this gives
01100001 01100010 01100011 01100100 01100101 1
Since L = 40, the number of bits in the above is 41 and K = 855 "0"s are appended, making the total now 896. This gives the following in hex:
61626364 65800000 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
The 128-bit representation of L = 40 is hex 00000000 00000000 00000000 00000028. Hence the final padded message is the following hex:
61626364 65800000 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 00000028