title: Python: How to calculate png crc value
body: <pre><code>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 &amp; 1:
              c = 0xedb88320L ^ (c &gt;&gt; 1)
          else:
              c = c &gt;&gt; 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)) &amp; 0xff)] ^ (c &gt;&gt; 8)
    return c
  
  # /* Return the CRC of the bytes buf[0..len-1]. */
  def crc(buf):
    return update_crc(0xffffffffL, buf) ^ 0xffffffffL
  </code></pre>
  
  <p>I used this code to calculate png crc value<br>
  My IHDR chunk data is <code>000008A0 000002FA 08020000 00</code> and the result of that code was <code>0xa1565b1L</code><br>
  However real crc was <code>0x84E42B87</code>. I checked this value with well known png checker tool and correct crc was <code>0x84E42B87</code>.<br>
  I can't understand how this value is calculated and correct value.</p>