How to implement CRC-32 in Python

I have already introduce to you about the basics of CRC and CRC-32 here and here. If you don’t know about CRC, please take a look at those guides.

There are some libraries to calculate CRC-32 in Python, 2 of them are: zlib and binascii. Here’re the sample codes:

Using zlib

For applications that require data compression, zlib module allow compression and decompression.

zlib has crc32 method to computes a CRC (Cyclic Redundancy Check) checksum of data. If value is present, it is used as the starting value of the checksum; otherwise, a fixed default value is used.

The algorithm is not cryptographically strong, and should not be used for authentication or digital signatures. Since the algorithm is designed for use as a checksum algorithm, it is not suitable for use as a general hash algorithm.

import zlib 
zlib.crc32(str.encode('hello from'))