Search code examples
sha

What is sha-256 padding?


Studying a source code of one of the cpu miners I found this piece of code:

work->data[20] = 0x80000000;

Well, I asked the coded about it and his answer was:

"Those values are part of standard SHA-2 padding"

Googling "sha 2 padding" didn't help. Can you tell me what is this for?

I thought that md5/sha256 functions simply take data and return hash.

I don't understand the "padding" concept.


Solution

  • You can find it documented in RFC 4634 -- SHA and HMAC-SHA, section 4.1. Quoting from the introduction of part 4:

    As a summary, a "1" followed by a number of "0"s followed by a 64-bit or 128-bit integer are appended to the end of the message.

    0x80000000 is 10000000000000000000000000000000 in binary. Here is your "1" followed by a number of "0"s.

    The reason is that SHA-256 processes the input by splitting it in blocks of fixed size. The last block might be smaller, so it is extended (padded) until its size matches the expected block size.