How can I create a fixed length output in my hash function? I've been recently looking into the creation (and theory) of hash functions, however I just can't figure out how to turn a message into something of a fixed length.

At the moment, my theory of a hash function is like so:


Take in the message (e.g. "secret")
Split it into different 'blocks' (e.g. "sec" + "ret")
Mix them up, and interact different blocks depending on what block type
they are (perhaps block type would be determined by mathematical calculations
on the character values, and then certain blocks would interact with other
blocks to merge in a weird way to create another block).


This creates a message which cannot be undone (since at the end, you don't know what the block types are - and even if you do, you don't know how they interacted to get to those blocks) - however it is not of a fixed length.

What would I do to create a fixed length digest?
Basically, typical hash functions produce a fixed-length output by the following method:


They expand the input to some multiple of their block size by adding padding.
They compress the first two blocks into a single block using a mixing function and repeat until only one block is left.
The use a fixed finalizing function to turn that last block into their output.


As a simple example, consider a rather trivial and insecure hash function for decimal numbers that produces a one decimal digit output. It could work like this:


Add a 9 onto the end of the input. Then add zeroes until the number of digits is even.
If there are more than two digits, replace the first four digits with the two-digit sum of those four digits. Repeat until only two digits remain.
Sum the final two digits to get the output.


This will produce a single decimal digit of output for any decimal input.

For example, take 123.


Add a 9 on the end - 1239. The number of digits are even, so no zeroes appended.
We replace the first four digits with their sum, 15. There are two digits, we're done.
We sum the 1 and 5 to get 6. Our output is 6.


While this function is simple and insecure (because its mixing function is so bad and its block size so small) it illustrates how hash functions produce a fixed-sized output from a variable-sized input.