MD5 AND SHA-1 HASH GENERATION

A hash function takes a message of any length and gives a fixed length
digest. It is one way (cannot be reversed) and a small change in input
changes the whole output (avalanche effect).

            MD5                     SHA-1
Digest      128 bit (32 hex)        160 bit (40 hex)
Block       512 bit                 512 bit
Rounds      64 (4 x 16)             80 (4 x 20)
Word size   32 bit                  32 bit

Common steps for both:
1. Padding: add a 1 bit, then zeros, until length = 448 mod 512, then add the 64 bit message length.
2. Divide into 512 bit blocks.
3. Initialise the chaining buffer (MD5: 4 words A,B,C,D, SHA-1: 5 words A,B,C,D,E).
4. Process each block through the rounds using logical functions, shifts and modular addition.
5. Output of the last block = hash.

SHA-1 word expansion: W[t] = ROTL1( W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16] )
SHA-1 step: TEMP = ROTL5(A) + f(B,C,D) + E + W[t] + K[t]
MD5 step: A = B + ROTL( A + F(B,C,D) + M[i] + K[i], s )

The program uses python hashlib to generate both hashes.
Both are considered broken for collisions now, so SHA-256 is used in practice.
