pull down to refresh

SHA-256, Part 1: PaddingSHA-256, Part 1: Padding

SHA-256 is used everywhere in Bitcoin.

We usually think of hashing as:

Input → [ Black Box ] → Hash

Let’s peek inside the black boxLet’s peek inside the black box

SHA-256 has three internal steps:

Input
  ↓
+----------------------+
|  Padding             |
|  Message schedule    |
|  Compression         |
+----------------------+
  ↓
Hash

Today we focus only on Padding.


Example inputExample input

We’ll hash the string:

"bitcoin"

Step 1: Convert characters to ASCIIStep 1: Convert characters to ASCII

'b' → 98  → 01100010
'i' → 105 → 01101001
't' → 116 → 01110100
'c' → 99  → 01100011
'o' → 111 → 01101111
'i' → 105 → 01101001
'n' → 110 → 01101110

Concatenate all bytes:

01100010011010010111010001100011
011011110110100101101110

Total: 7 bytes = 56 bits


Step 2: Append a 1 bitStep 2: Append a 1 bit

This marks the end of the message:

01100010011010010111010001100011
0110111101101001011011101

(…1 bit added at the end)


Step 3: Add lengthStep 3: Add length

Compute original message length:

Message: "bitcoin"
Chars: 7
Bits/char: 8
Total bits = 7 × 8 = 56

Convert 56 to a 64-bit big-endian binary:

0000000000000000000000000000000000000000000000000000000000111000

Append it (big-endian):

[message]1 [zeros 😴] [64-bit length]

But we’re not done yet…But we’re not done yet…

SHA-256 works in 512-bit blocks

Currently we have:

Message: 56 bits
"1" marker: 1 bit
Length: 64 bits
----------------------
Total: 121 bits

We need 512 bits total:

512 − 121 = 391 zero bits

So we add 391 zeros between the marker and the length.


Final padded blockFinal padded block

Structure:

[ 56-bit message ]
[ 1-bit "1" marker ]
[ 391 zeros ]
[ 64-bit length ]

This forms one full 512-bit block


Next step: Message scheduleNext step: Message schedule

Now we take the padded block and pass it through the message schedule, which expands it into W[0..63] for compression.

You can watch this padding step live at:

https://hashexplainer.com

Repo: https://github.com/bitcoin-dev-project/hashes-visualizer