hashcryptographyhash-collisionhash-code-uniqueness

Is a hash result ever the same as the source value?


This is more of a cryptography theory question, but is it possible that the result of a hash algorithm will ever be the same value as the source? For example, say I have a string:

baf34551fecb48acc3da868eb85e1b6dac9de356

If I get the SHA1 hash on it, the result is:

4d2f72adbafddfe49a726990a1bcb8d34d3da162

In theory, is there ever a case where these two values would match? I'm not asking about SHA1 specifically here - it's just my example. I'm just wondering if hashing algorithms are built in such a way as to prevent this.


Solution

  • Well, it would depend on the hashing algorithm - but I'd be surprised to see anything explicitly prevent this. After all, it really shouldn't matter.

    I suspect it's very unlikely to happen, of course (for cryptographic hashes)... but even if it does, that shouldn't cause a problem.

    For non-crypto hashes (used in hash tables etc) it would be perfectly reasonable to return the source value in some cases. For example, in Java, Integer.hashCode() just returns the embedded value.