Python:如何计算png crc值

Python:如何计算png crc值,python,image,image-processing,crc,crc32,Python,Image,Image Processing,Crc,Crc32,我使用此代码计算png crc值 我的IHDR区块数据是000008A0000002FA 08020000,该代码的结果是0xa1565b1L 然而,真正的crc是0x84E42B87。我用著名的png检查工具检查了这个值,正确的crc是0x84E42B87 我不明白这个值是如何计算的,也不知道正确的值。CRC是通过块类型和数据计算的,而不仅仅是数据。因此,这些字节前面将有四个字节IHDR。然后得到正确的CRC 顺便说一句,我不知道您是如何从000008A0 000002FA 08020000获

我使用此代码计算png crc值
我的IHDR区块数据是
000008A0000002FA 08020000
,该代码的结果是
0xa1565b1L

然而,真正的crc是
0x84E42B87
。我用著名的png检查工具检查了这个值,正确的crc是
0x84E42B87


我不明白这个值是如何计算的,也不知道正确的值。

CRC是通过块类型和数据计算的,而不仅仅是数据。因此,这些字节前面将有四个字节
IHDR
。然后得到正确的CRC

顺便说一句,我不知道您是如何从
000008A0 000002FA 08020000
获得
0xa1565b1L
。我得到
0xa500050a
作为这些字节的CRC。你肯定还有别的地方做错了。您需要提供一个完整的示例,以便我们能够判断

crc_table = None

def make_crc_table():
  global crc_table
  crc_table = [0] * 256
  for n in xrange(256):
    c = n
    for k in xrange(8):
        if c & 1:
            c = 0xedb88320L ^ (c >> 1)
        else:
            c = c >> 1
    crc_table[n] = c

make_crc_table()

"""
/* Update a running CRC with the bytes buf[0..len-1]--the CRC
should be initialized to all 1's, and the transmitted value
is the 1's complement of the final running CRC (see the
crc() routine below)). */
"""
def update_crc(crc, buf):
  c = crc
  for byte in buf:
    c = crc_table[int((c ^ ord(byte)) & 0xff)] ^ (c >> 8)
  return c

# /* Return the CRC of the bytes buf[0..len-1]. */
def crc(buf):
  return update_crc(0xffffffffL, buf) ^ 0xffffffffL