Skip to content
Message Padding
step 1/5

Reading — step 1 of 5

Read

~1 min readPadding & Length

Message Padding

SHA-256 requires the input to be a multiple of 512 bits (64 bytes). For arbitrary-length input:

pad(message):
    L = bit length of message
    1. Append a single bit '1'  (=== byte 0x80)
    2. Append zero bits until length ≡ 448 (mod 512)
    3. Append L as a 64-bit big-endian integer

After padding the length is ≡ 0 (mod 512).

Why these specific values?

  • The 1 bit + zeros gives a unique boundary so different-length messages with the same prefix don't collide.
  • 64-bit length forces the hash to commit to length up front (defends against length-extension attacks for SHA-256 itself, though SHA-256 is still length-extendable — use HMAC if you need MACs).

Example: hashing "abc" (3 bytes = 24 bits):

input bytes:  61 62 63
+ 1 bit:      80
+ zeros:      00 00 00 ... 00     (until total = 56 bytes)
+ 64-bit len: 00 00 00 00 00 00 00 18    (24 bits)
total:        64 bytes

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…