What is SHA-256?
SHA-256 (Secure Hash Algorithm 256-bit) is a cryptographic hash function that generates a fixed-size 256-bit (32-byte) hash value for any given input, no matter the size. It is widely used in blockchain, digital signatures, and various security protocols. Here's how SHA-256 works under the hood, broken down into simple steps:
1. Message Preprocessing
- Input Data: SHA-256 takes an input of any length (text, file, etc.).
- Padding: The input message is
padded to ensure its length is a multiple of 512
bits. Padding is done by adding a '1' bit followed
by enough '0' bits, and finally, the length of the
original message in 64-bit binary is appended.
Example: If the input is 448 bits, SHA-256 adds padding to make it 512 bits, which is the required block size.
2. Message Parsing
The padded message is then divided into 512-bit blocks. If the message is larger than 512 bits, it is split into multiple blocks. Each block is processed one at a time.
3. Initializing Hash Values
SHA-256 uses a set of 8 constant initial hash values (H0 to H7). These are fixed hexadecimal numbers that are defined as part of the algorithm:
- H0 = 6a09e667
- H1 = bb67ae85
- H2 = 3c6ef372
- H3 = a54ff53a
- H4 = 510e527f
- H5 = 9b05688c
- H6 = 1f83d9ab
- H7 = 5be0cd19
4. Processing the Message Block-by-Block
Each 512-bit block is processed through 64 iterations (or rounds) using mathematical functions and bitwise operations. A message schedule of 64 words (32 bits each) is created from the 512-bit block. The first 16 words are taken directly from the block, and the remaining 48 words are derived from the previous words using bitwise operations like shifts and XORs.
5. Compression Function
The heart of SHA-256 is the compression function, which involves mixing the message schedule and the hash values. In each of the 64 rounds, SHA-256 uses constants (called K0, K1, ..., K63) and applies bitwise logical operations (such as AND, OR, XOR) and rotations on the working variables. The current hash values are updated based on this process, and the results are fed back into the system for the next round.
6. Update the Hash Values
After processing all 64 rounds for a 512-bit block, the algorithm updates the initial hash values (H0 to H7) by adding the outputs from the compression function to the previous values of H0 to H7. This "chaining" process continues for each 512-bit block of the message.
7. Final Hash Value
After all blocks have been processed, the final 256-bit hash is produced by concatenating the updated H0 to H7 values. This final value is the hash digest of the input message.
Example (Simplified Overview):
- Input message: "hello"
- Convert "hello" into binary and apply padding.
- Divide the binary message into 512-bit blocks.
- Process each block with 64 rounds of mixing, shifting, and rotating.
- At the end, the initial hash values (H0-H7) are
updated to form the final 256-bit hash value:
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
Key Components:
- Chaining: Each block influences the final hash, creating a dependency between the blocks.
- Bitwise Operations: Core operations like XOR, rotations, and shifts ensure non-reversibility and randomness.
- Fixed Output: Regardless of input size, the output is always 256 bits (32 bytes).
Summary:
SHA-256 takes any input, processes it through multiple rounds of bitwise operations, and outputs a 256-bit hash. This makes it suitable for verifying data integrity and securing sensitive information like passwords and blockchain transactions. Its cryptographic strength comes from the complexity of the operations and the difficulty in reversing the process.