Skip to content

Instantly share code, notes, and snippets.

@mildsunrise

mildsunrise/reverse_crc.py

Last active Dec 12, 2020
Embed
What would you like to do?
calculate the seed, given a message and its expected CRC32
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