libscrc
libscrc copied to clipboard
Implement GSM32
Is it possible to implement CRC-32 calculation of GSM32 like in this document: link page 140?
OK, No problem.
Please provide me with some data and the CRC32 results for this data. I want to verify the calculation results.
Above, Thank you.
polynomial = 0x04C11DB7L
def crc32_gsm(data: bytes) -> int:
# Generator polynomial
POLY = 0x04C11DB7
# Initial remainder
crc = 0x00000000
# Process each byte in the data
for byte in data:
crc ^= byte << 24 # Align byte to the high end of the current CRC value
for _ in range(8): # Process each bit in the byte
if crc & 0x80000000: # If the highest bit is set
crc = (crc << 1) ^ POLY
else:
crc <<= 1
crc &= 0xFFFFFFFF # Ensure crc remains within 32-bit bounds
return crc
def rearrange_data(data: bytes) -> bytes:
""" Rearrange data into the format required by the CRC algorithm """
words = []
for i in range(0, len(data), 2):
if i + 1 < len(data):
mso = data[i]
lso = data[i + 1]
else:
mso = data[i]
lso = 0
words.append(lso)
words.append(mso)
return bytes(words)
# Example usage
data = b'Test data for GSM32 CRC'
rearranged_data = rearrange_data(data)
checksum = crc32_gsm(rearranged_data)
print(f'GSM32 CRC checksum: {checksum:08X}')