1
$\begingroup$

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).

$\endgroup$
2
  • 1
    $\begingroup$ 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? $\endgroup$ Feb 27 '21 at 1:15
  • $\begingroup$ Note that that function has nothing to do with Cryptographic hash functions. Collisions findings and pre-image attacks are imminent. $\endgroup$
    – kelalaka
    Feb 28 '21 at 13:49
1
$\begingroup$

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).

$\endgroup$
3
  • $\begingroup$ So the output size can be determined before we use the hash function? $\endgroup$
    – Brown
    Feb 23 '21 at 6:13
  • 1
    $\begingroup$ @Vector You already noted that the output size is 256 bits, so yes, it's known in advance. $\endgroup$
    – D.W.
    Feb 23 '21 at 6:16
  • 2
    $\begingroup$ 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. $\endgroup$
    – gnasher729
    Feb 23 '21 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.