How SHA-256 Processes Data in Binary Chunks

Cryptographic hash functions like SHA-256 transform arbitrary amounts of data into fixed-size 256-bit digests by utilizing the Merkle–Damgård construction. To achieve this, the algorithm converts input text or files into raw binary, applies deterministic padding to ensure the total bit length is a multiple of 512, divides the bitstream into sequential 512-bit blocks, and repeatedly feeds them through an internal compression function using bitwise operations.

1. Binary Conversion and Padding

Before processing begins, the input message is interpreted strictly as a sequence of binary digits (0s and 1s). Because real-world messages come in arbitrary lengths, SHA-256 applies padding to make the total bit length an exact multiple of 512 bits.

The padding follows a strict standard: 1. A single 1 bit is appended to the end of the binary message. 2. A series of 0 bits is appended until the total length is exactly 64 bits short of a multiple of 512. 3. The original message’s length (measured in bits before padding) is appended as a 64-bit big-endian binary integer, filling the remaining space.

2. Dividing into 512-Bit Blocks

Once padded, the complete binary string is partitioned into distinct, consecutive 512-bit chunks (\(M_1, M_2, \dots, M_n\)). Each 512-bit chunk is processed sequentially. If a message is short, it may only occupy a single 512-bit chunk; if it is large, it will span thousands or millions of consecutive chunks.

3. Message Schedule Expansion

For each 512-bit chunk, the algorithm breaks the block down into sixteen 32-bit words (\(W_0\) through \(W_{15}\)). It then expands these 16 words into a schedule of 64 words (\(W_0\) to \(W_{63}\)) using binary shift and rotation functions (\(\sigma_0\) and \(\sigma_1\)) combined with bitwise XOR and modular addition (\(+ \pmod{2^{32}}\)). This expansion guarantees that every bit within the 512-bit chunk influences multiple stages of the compression cycle.

4. Iterative Compression Rounds

SHA-256 maintains eight internal 32-bit working variables (labeled a through h), which are initialized to fixed constants derived from the fractional parts of square roots of the first eight prime numbers.

For each 512-bit chunk, the algorithm runs 64 processing rounds: * In round \(t\), working variables are mixed using bitwise logical operations: Ch (choose), Maj (majority), and right-rotations (\(\Sigma_0, \Sigma_1\)). * The expanded word \(W_t\) and a round-specific constant \(K_t\) (derived from the cube roots of primes) are added to the working variables using addition modulo \(2^{32}\). * The variables cycle down, updating a through h with newly calculated values.

5. State Accumulation and Final Digest

After completing all 64 rounds for a given chunk, the newly calculated values of a through h are added to the previous state values. The resulting eight 32-bit integers serve as the baseline input for the next 512-bit chunk.

Once the final 512-bit block has been processed, the eight 32-bit registers are concatenated in order (a through h) to produce the final, irreversible 256-bit binary hash digest.