4

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

4 回答 4

8

好吧,这将取决于散列算法 - 但我会惊讶地看到任何明确阻止这种情况的东西。毕竟,真的应该无所谓。

我怀疑它当然不太可能发生(对于加密哈希)......但即使它发生了,也不应该引起问题。

对于非加密哈希(用于哈希表等),在某些情况下返回源值是完全合理的。例如,在 Java 中,Integer.hashCode()只返回嵌入的值。

于 2009-09-04T15:21:34.130 回答
4

当然,整数的 Python 散列算法返回整数的值。所以哈希(1)== 1。

于 2009-09-04T15:25:40.130 回答
4

给定一个好的散列算法,返回一个看似随机的输出,我相信平均应该有一个输入将自己作为输出。假设哈希可以给出 N 个可能的输出。这意味着有 N 个可能的输入是可能的。对于其中的每一个,输出匹配输入的几率是 1/N,因此预期的固定点数是 N*1/N,或 1。

于 2009-09-04T15:37:28.783 回答
2

可以定义散列函数来避免散列(x)==x 的“固定点”,但您的散列奎因略有不同,因为您采用散列的十六进制字符串表示而不是原始二进制。我认为,设计一个可能会挫败它的哈希是不可行的,而且它在数学上不太有趣,因为它取决于 0-F 到 ASCII 字符代码的任意映射。

请参阅是否存在 md5(x) == x 的 MD5 固定点?有关 MD5 中固定点的讨论。对于 hex hash-quines 和任何其他具有 128 位输出的散列函数,概率计算同样适用。

于 2009-09-04T15:40:50.353 回答