是否可以分片进行CRC-32计算?
我使用这个简单的函数来计算给定文件的 CRC 校验和:
long i, j = 0;
int k = 0;
uint crc = 0xFFFFFFFF;
FileInfo file_info = new FileInfo(file);
byte[] file_buffer = new byte[32768];
FileStream file_stream = new FileStream(@file, FileMode.Open);
while ((i = file_stream.Read(file_buffer, 0, file_buffer.Count())) > 0)
{
for (j = 0; j < i; j++)
{
uint before = crc;
k = (int)((crc ^ file_buffer[j]) & 0x000000FFL);
uint after = (uint)((crc >> 8) & 0x00FFFFFFL) ^ crc32_table[k];
crc = after;
uint test = (uint)((crc << 8) & 0x00FFFFFFL) ^ crc32_table[k];
MessageBox.Show((~crc).ToString("X"));
}
}
file_stream.Close();
return ~crc;
我的问题是:假设我有一个大文件,比如说 100MB。前 50MB 和最后 50MB 的 CRC-32 计算与 100MB 文件的 CRC-32 计算之间是否有任何联系?
我问的原因是我有一些非常大的文件(〜10GB给予或接受)需要一些时间才能生成,但是在生成它们时,大多数部分保持静态,但是中间的部分(已知点)和开头(标题,也称为部分/长度)。计算 10GB 文件的 CRC-32 校验和需要相当长的时间,所以我想知道是否有任何方法可以分块进行?
I use this trivial function to calculate the CRC checksum of a given file:
long i, j = 0;
int k = 0;
uint crc = 0xFFFFFFFF;
FileInfo file_info = new FileInfo(file);
byte[] file_buffer = new byte[32768];
FileStream file_stream = new FileStream(@file, FileMode.Open);
while ((i = file_stream.Read(file_buffer, 0, file_buffer.Count())) > 0)
{
for (j = 0; j < i; j++)
{
uint before = crc;
k = (int)((crc ^ file_buffer[j]) & 0x000000FFL);
uint after = (uint)((crc >> 8) & 0x00FFFFFFL) ^ crc32_table[k];
crc = after;
uint test = (uint)((crc << 8) & 0x00FFFFFFL) ^ crc32_table[k];
MessageBox.Show((~crc).ToString("X"));
}
}
file_stream.Close();
return ~crc;
My question is this: Say I have a large file, of say 100MB. Is there any link between a CRC-32 calculation of the first 50MB and the last 50MB and the CRC-32 calculation of the 100MB file?
The reason I'm asking, is I have some very large files (~10GB give or take) which take some time to generate, but while they're being generated, most parts remain static, however, parts in the middle (known point) and right at the start (header, also known part/length). Calculating a CRC-32 checksum of a 10GB file takes quite some time, so I was wondering if there was any way to do it in chunks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的。请参阅
crc32_combine_()
zlib 为例。Yes. See
crc32_combine_()
in zlib for an example.确实可以并行化 CRC-32 计算,但细节很混乱,我需要花大约一天的时间来编写代码。
让我们看一下基本的 CRC 算法,其中没有求反,也没有位反转。
对于要计算 CRC 的字节串,我们将其称为消息。基本思想是,将消息视为 GF(2) 中的多项式,然后计算 CRC 多项式的余数。
基本 CRC 算法是加法/线性的。如果有两条长度相同的消息 a 和 b,则 CRC(a XOR b) = CRC(a) XOR CRC(b)。
此外,如果在消息右侧填充 n 个零,则新的 CRC 将是旧的 CRC 乘以 x^n mod CRC 多项式。
话虽如此,解决问题的唯一方法是真正理解 CRC 算法背后的数学原理并编写自己的自定义代码。这是 CRC 的一个很长但非常完整的解释:http://www.ross.net/crc /download/crc_v3.txt
It is indeed possible to parallelize the CRC-32 computation, but the details are messy and I'd need to spend about a day to come up with the code.
Let's look at the basic CRC algorithm, where there is no negation and no bit reversal.
For the string of bytes that you want to compute the CRC of, let's call it the message. The basic idea is that you treat the message as a polynomial in GF(2), and you compute its remainder modulo the CRC polynomial.
The basic CRC algorithm is additive/linear. If you have two messages a and b of the same length, then CRC(a XOR b) = CRC(a) XOR CRC(b).
Furthermore, if you pad the message on the right side with n zeros, the new CRC will be the old CRC times x^n mod the CRC polynomial.
With all that said, the only way to solve your problem is to really understand the mathematics behind the CRC algorithm and write your own custom code. Here's a long but very complete explanation of CRCs: http://www.ross.net/crc/download/crc_v3.txt
这个方程是关键:
假设您要计算以下消息的 CRC:
存在计算 CRC 的函数:
如果
crc_part1
和crc_part2
补零 (\0
)他们的参数如图所示,crc_join
只是异或。可以使用查找表在
crc_part1
中解释尾随零。 crc_part2 中的前导零可以被忽略。参考文献:
永州。做吧,宋禄。尹泰奎。金光义. Pyun 和 Sin-Chong。公园
This equation is key:
Suppose you want to compute the CRC of the following message:
There exist functions to compute the CRC as:
If
crc_part1
andcrc_part2
zeros pad (\0
) their arguments as shown,crc_join
is just XOR.The trailing zeros can be accounted for in
crc_part1
with lookup tables. The leading zeros can be ignored incrc_part2
.References:
Youngju. Do, Sung-Rok. Yoon, Taekyu. Kim, Kwang Eui. Pyun and Sin-Chong. Park
我知道这是一个老问题,但这是我用于“分块”CRC 计算的方法,以防对任何人有帮助:
I know this is an old question but this is what I use for "chunking" CRC calculations in case this helps anyone: