Last active
January 10, 2022 00:21
-
-
Save mildsunrise/59aacd7fe9b07932c962b0e0c3dfc57c to your computer and use it in GitHub Desktop.
calculate the seed, given a message and its expected CRC32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from zlib import crc32 | |
from polynomial_arithmetic import p_mod_mul, p_mult_inv, p_mod_pow | |
reverse32 = lambda x: int('{:032b}'.format(x)[::-1], 2) | |
rev_neg_32 = lambda x: reverse32(x ^ ((1 << 32) - 1)) | |
# generic (mathematical version): get the required seed given the actual and expected CRC, the polynomial and message size | |
get_seed = lambda msgbits, checksum1, checksum2, poly: \ | |
p_mod_mul(checksum1 ^ checksum2, p_mod_pow(p_mult_inv(2, poly), msgbits, poly), poly) | |
# specific version for CRC-32, which reverses and negates the seed / CRCs | |
get_seed_crc32 = lambda msg, checksum: rev_neg_32(get_seed( \ | |
len(msg)*8, rev_neg_32( crc32(msg, rev_neg_32(0)) ), rev_neg_32(checksum), 0x104c11db7)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment