How to calculate CRC32 with Python to match online results?

前端 未结 3 740
盖世英雄少女心
盖世英雄少女心 2020-12-13 19:19

I\'m trying to calculate/generate the CRC32 hash of some random strings using Python but they do not match the values I generate from online sources. Here is what I\'m doing

相关标签:
3条回答
  • 2020-12-13 19:31

    zlib.crc32 documentation suggests using the following approach "to generate the same numeric value across all Python versions and platforms".

    import zlib
    hex(zlib.crc32(b'hello-world') & 0xffffffff)
    

    The result is 0xb1d4025b as expected.

    0 讨论(0)
  • 2020-12-13 19:41

    It seems that python is returning an signed integer (hence the negative number), whereas the others are returning an unsigned integer.

    I have tried using a modulus with 2^32, and it gave the same value as these sites.

    >>> hex(zlib.crc32(b'hello-world')% 2**32)
    '0xb1d4025b'
    
    0 讨论(0)
  • 2020-12-13 19:50

    Python 2 (unlike py3) is doing a signed 32-bit CRC.

    Those sites are doing an unsigned 32-bit CRC.

    The values are the same otherwise, as you can see from this:

    >>> 0x100000000 - 0xb1d4025b == 0x4e2bfda5
    True
    

    One quick way to convert from 32-bit signed to 32-bit unsigned is:*

    >>> -1311505829 % (1<<32)
    2983461467
    

    Or, in hex:

    >>> hex(-1311505829 % (1<<32))
    '0xb1d4025b'
    

    & 0xFFFFFFFF or % 0x100000000 or & (2**32-1) or % (2**32) and so on are all equivalent ways to do the same bit-twiddling; it just comes down to which one you find most readable.


    * This only works in languages that do floored integer division, like Python (-3 // 2 == -2); in languages that do truncated integer division, like Java (-3 / 2 == -1), you'll still end up with a negative number. And in languages that don't even require that division and mod go together properly, like C, all bets are off—but in C, you'd just cast the bytes to the type you want…

    0 讨论(0)
提交回复
热议问题