# Why mod operation can be used as hash function

I'm new here, and I'm currently trying to understand why mod operation can be used as a hash function.

For example, consider the function $$h(x)=x\mod 2^{256}$$, where $$x$$ can be a string of any length. The book says that this function will return a fixed size of 256 bits. To my understanding, $$h(x)$$ will return an integer in the interval $$[0,2^{256})$$, which can be expressed in 256 bits. My question is that, suppose $$x$$ is $$5$$, then $$h(x)=5$$, which can also be expressed in 3 bits (101). How do we guarantee that the output will be the size of $$256$$ bits? (I am just new to computer science and apologize if the question is too elementary).

• How does your car guarantee that the odometer is always 7 digits even though you only drove less than 10000 miles since you bought it? – Jörg W Mittag Feb 27 at 1:15
• Note that that function has nothing to do with Cryptographic hash functions. Collisions findings and pre-image attacks are imminent. – kelalaka Feb 28 at 13:49

## 1 Answer

The output of the function can always be expressed as a 256-bit value. The value 5 is expressed as 00000...000101 (some 0's omitted).

• So the output size can be determined before we use the hash function? – Brown Feb 23 at 6:13
• @Vector You already noted that the output size is 256 bits, so yes, it's known in advance. – D.W. Feb 23 at 6:16
• Of course your hash function is no good at all, since it ignores all but the last 256 bit. For example, if you calculate hash codes for books containing fairy tales, anything ending in ''and this, dear children, is how the story ends, and they lived happily ever after.' would have the same hash code. – gnasher729 Feb 23 at 22:06