An attempt to implement SHA-256 from scratch using golang and reverse engineer.
Algorithm:
-
Intialization of Hash Values(H0-H7) and the constants (K0-K63) - done
-
Padding to ensure the message is near a 512 bits multiple. -- workflow of padding:
- add one "1" after converting the message to binary
- add "0"s till it satisfies 448(bits) mod 512(bits). In layman terms, there must be 64 bits space left after adding "0"s in the nearest 512 mulitple.
- In the end appending the 64-bit representation to the message we received.
-
Parsing the message by sptlitting it into 512-bit blocks. W[0]-W[63]
-
Scheduling the message: -- workflow of scheduling:
-
Compression Function -- workflow of compression:
-
Updating Hash after every 512-bit round:
- H0 = H0 + a
- H1 = H1 + b
- H2 = H2 + c
- H3 = H3 + d
- H4 = H4 + e
- H5 = H5 + f
- H6 = H6 + g
- H7 = H7 + h
-
Finally, Cocatenate the hash values to get the final 256-bit hash.