【问题标题】:Reversing CRC32 / Removing bytes from CRC32反转 CRC32 / 从 CRC32 中删除字节
【发布时间】:2020-03-27 01:06:52
【问题描述】:

我有一个应用程序可以在一些长度为 l 的数据流上计算 crc32。但是,我想从最终的 crc32 结果中删除我 crc'ed 的最后 4 个字节,这意味着我实际上希望结果是超过长度的数据的 crc32 (l-4)。有没有有效的方法来做到这一点?

编辑: 我知道我要排除的最后 4 个字节。

【问题讨论】:

  • 你还知道最后4个字节吗?为什么不能在到达L - 4 时停止散列?
  • 我知道我想排除的最后 4 个字节。在正常数据传输期间,我得到一个 231 字节的数据流。最后一次传输可以占用从 1 到 231 字节的任意数量的字节。所以如果最后一次传输只有 1 个字节长,我的 crc32 已经包含了 3 个我不想包含的字节。

标签: hash crc crc32


【解决方案1】:

是的,这是可能的。

首先,CRC 是线性的,因此我们可以通过计算 crcOfData ^ crc(last4Bytes) 来找到如果最后 4 个字节为 0 时的 CRC 值。不过,根据您的 CRC 的详细信息,会有一些细微的变化。

其次,“删除最后一位假设为零”的动作可以用一个32x32的布尔矩阵来建模,即:

uint32_t inv1[32];
uint32_t row = 2;
for (int n = 0; n < 31; n++) {
    inv1[n] = row;
    row <<= 1;
}
inv1[31] = 0x05EC76F1; // reciprocal of your crc polynomial (I used the one that matches _mm_crc32)

“删除 32 个零位”的矩阵可以通过对矩阵进行几次平方来找到:

uint32_t inv[32];
gf2_matrix_square(inv, inv1); // 2
gf2_matrix_square(inv1, inv); // 4
gf2_matrix_square(inv, inv1); // 8
gf2_matrix_square(inv1, inv); // 16
gf2_matrix_square(inv, inv1); // 32


uint32_t gf2_matrix_times(uint32_t *mat, uint32_t vec)
{
    uint32_t sum = 0;
    while (vec) {
        if (vec & 1)
            sum ^= *mat;
        vec >>= 1;
        mat++;
    }
    return sum;
}

void gf2_matrix_square(uint32_t *square, uint32_t *mat)
{
    for (int n = 0; n < 32; n++)
        square[n] = gf2_matrix_times(mat, mat[n]);
}

由于将该矩阵平方 5 次与数据无关,因此您可以对结果进行硬编码。

使用gf2_matrix_times(inv, crcOfData ^ crc(last4Bytes)) 可以找到实际的“删除 4 个字节”,例如只是为了验证它是否有效:

auto crc0 = _mm_crc32_u32(0, 0xDEADBEEF);
auto crc1 = _mm_crc32_u32(crc0, 0xCAFEBABE);
auto undo = gf2_matrix_times(inv, crc1 ^ _mm_crc32_u32(0, 0xCAFEBABE));

【讨论】:

    猜你喜欢
    • 2012-03-06
    • 2021-11-15
    • 2021-08-07
    • 2019-12-04
    • 2018-07-05
    • 2013-09-09
    • 1970-01-01
    • 2020-04-03
    相关资源
    最近更新 更多