-
binascii.
crc32
(
data
[ ,
crc
]
)
¶
-
Compute CRC-32, the 32-bit checksum of data, starting with an initial crc. This is consistent with the ZIP file checksum. Since the algorithm is designed for use as a checksum algorithm, it is not suitable for use as a general hash algorithm. Use as follows:
print binascii . crc32 ( "hello world" )
# Or, in two pieces:
crc = binascii . crc32 ( "hello" )
crc = binascii . crc32 ( " world" , crc ) & 0xffffffff
print 'crc32 = 0x %08x ' % crc
Note
To generate the same numeric value across all Python versions and platforms use crc32(data) & 0xffffffff. If you are only using the checksum in packed binary format this is not necessary as the return value is the correct 32bit binary representation regardless of sign.
Changed in version 2.6: The return value is in the range [-2**31, 2**31-1] regardless of platform. In the past the value would be signed on some platforms and unsigned on others. Use & 0xffffffff on the value if you want it to match 3.0 behavior.
Changed in version 3.0: The return value is unsigned and in the range [0, 2**32-1] regardless of platform.
Computes a cyclic redundancy check value and returns a 32-bit unsigned value. The result is NULL
if the argument is NULL
. The argument is expected to be a string and (if possible) is treated as one if it is not.
mysql>SELECT CRC32('MySQL');
-> 3259397556
mysql>SELECT CRC32('mysql');
-> 2501908538