【问题标题】:Distribution of CRC checksumsCRC校验和的分布
【发布时间】:2016-11-13 21:16:25
【问题描述】:
我正在调查 CRC 校验和在用作哈希时的碰撞概率。我知道如何计算均匀分布的哈希算法的碰撞概率(这意味着获得随机输入数据的所有可能校验和的机会是相同的)。
我不知道的(我在网上找不到):
- CRC 校验和通常 [not] 均匀分布吗?
- 分布是否取决于多项式?
- 分布是否取决于输入数据的大小?
P.S.:我知道使用 CRC 作为哈希时的限制,所以这不是这个问题的一部分。
【问题讨论】:
标签:
hash
distribution
crc
hash-collision
【解决方案1】:
除了恶意(您可以通过更改消息中的位来强制执行任何您喜欢的 CRC),CRC 均匀分布在所有值上。多项式无关紧要,只要是有效的 CRC 多项式,输入只需为 CRC 的大小或更大即可。
【解决方案2】:
我也很好奇这个,所以我在Linux上使用crc32命令做了一些测试:
# I am printing each number several times so the data is longer than 32-bits:
$ for N in {000001..999999}; do echo -n $N$N$N$N | crc32 /dev/stdin; done >crcs
# There are no complete (8-character) collisions:
$ cat crcs | sort | uniq -d | wc -l
0
# There are no 7-character collisions:
$ for COL in 1 2; do cat crcs | awk "{print substr(\$1,$COL,7)}" | sort | uniq -d; done | wc -l
0
# There are exactly 32k 6-character collisions:
$ for COL in 1 2 3; do cat crcs | awk "{print substr(\$1,$COL,6)}" | sort | uniq -d; done | wc -l
32768
# Also, the distribution of the letters in each column is *extremely* uniform.
# Each column has results similar to these:
$ cat crcs | awk '{print substr($1,1,1)}' | sort | uniq -c
62440 0
62439 1
62440 2
62440 3
62560 4
62560 5
62560 6
62560 7
62560 8
62560 9
62560 a
62560 b
62440 c
62440 d
62440 e
62440 f
...所以我的结论是 CRC32 在平均分配校验和方面做得非常好。