Is a hash result ever the same as the source value

2019-05-07 09:07发布

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.

4条回答
The star\"
2楼-- · 2019-05-07 09:46

A hash function might be defined to avoid ‘fixed points’ where hash(x)==x, but your hash-quine differs a little in that you're taking the string representation in hex of the hash rather than the raw binary. It would, I think, be infeasible to design a hash that could frustrate that, and it's mathematically less interesting since it depends on the arbitrary mapping of 0-F to ASCII character codes.

See Is there an MD5 Fixed Point where md5(x) == x? for a discussion about fixed points in MD5. The probability calculation would be equally true for hex hash-quines and any other hash function with 128 bits of output.

查看更多
来,给爷笑一个
3楼-- · 2019-05-07 09:50

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.

查看更多
地球回转人心会变
4楼-- · 2019-05-07 09:50

Sure, the Python hashing algorithm for integers returns the value of the integer. So hash(1) == 1.

查看更多
Fickle 薄情
5楼-- · 2019-05-07 09:52

Given a good hashing algorithm, one that returns a seemingly random output, I believe there should be on average one input that gives itself as the output. Let's say the hash can give N possible outputs. That means there are N possible inputs for which this is possible. For each of those, the odds of the output matching the input is 1/N, so there the expected number of fixed points is N*1/N, or 1.

查看更多
登录 后发表回答