How 7-Zip Calculates SHA-256 via Context Menu

This article explains how 7-Zip calculates SHA-256 checksums directly from the Windows context menu. It covers how the Windows Shell extension initiates the request, how 7-Zip reads the targeted file in memory-efficient blocks, how the cryptographic hashing algorithm processes the binary stream using hardware-accelerated instructions, and how the final hash is formatted and presented to the user.

Windows Shell Extension Activation

When you right-click a file in Windows Explorer, the operating system queries registered shell extensions to populate the context menu. 7-Zip implements this through a dedicated dynamic link library (such as 7-zip.dll). When you hover over the "CRC SHA" submenu and select "SHA-256", Windows passes the handle and path of the selected file directly to 7-Zip's context menu handler.

Streamed File Reading and Buffering

To maintain low memory usage and avoid system freezes, 7-Zip does not load the entire file into RAM at once. Instead, it accesses the file via standard OS read operations (CreateFile and ReadFile on Windows) and streams the data sequentially:

  • Buffer Allocation: 7-Zip allocates a dedicated memory buffer (typically between 64 KB and several megabytes).
  • Chunk Processing: The file is read chunk by chunk into this buffer. This allows 7-Zip to calculate hashes for multi-gigabyte or terabyte-sized files without exhausting system memory.

The SHA-256 Algorithm Execution

As each block of data enters the buffer, 7-Zip passes it to its internal SHA-256 implementation, based on the FIPS 180-4 standard:

  1. Initialization: The algorithm initializes eight 32-bit working variables with fixed, standard initial hash values.
  2. Block Processing: The data is broken down into 512-bit (64-byte) message blocks. For each block, 64 rounds of bitwise logical functions, modular additions, and cyclic shifts are performed to mutate the internal state.
  3. Hardware Acceleration: 7-Zip includes assembly-level optimizations. On modern CPUs supporting the Intel SHA Extensions (or ARMv8 Crypto instructions), 7-Zip executes dedicated hardware instructions (SHA256RNDS2, SHA256MSG1, SHA256MSG2) rather than pure software calculations. This increases hashing speeds to multiple gigabytes per second.
  4. Padding and Finalization: Once the end of the file is reached, standard cryptographic padding (a 1 bit followed by 0 bits and the 64-bit integer representation of the original file size) is applied to the final block, finalizing the internal state into the 256-bit digest.

Presenting the Result

After the final block is processed, 7-Zip converts the 256-bit binary digest into a 64-character hexadecimal string. It then spawns a lightweight GUI dialog box displaying the final SHA-256 hash, the total byte count of the processed file, and buttons allowing you to copy the hash directly to your clipboard.