title: Reversing CRC32
body: <p>I found a code to reverse CRC32 but I don't know how it works, because im not that good in programming, I just started. I just want to compare 2 files, the old and the new one, then in the new fix the CRC32 adding 4 bytes at the end of the file, so the 2 files will have the same CRC32. Here is the code, is in C#:</p>
  
  <pre><code>public class Crc32
  {
      public const uint poly = 0xedb88320;
      public const uint startxor = 0xffffffff;
  
      static uint[] table = null;
      static uint[] revtable = null;
  
      public void FixChecksum(byte[] bytes, int length, int fixpos, uint wantcrc)
      {
          if (fixpos + 4 &gt; length) return;
  
          uint crc = startxor;
          for (int i = 0; i &lt; fixpos; i++) {
              crc = (crc &gt;&gt; 8) ^ table[(crc ^ bytes[i]) &amp; 0xff];
          }
  
          Array.Copy(BitConverter.GetBytes(crc), 0, bytes, fixpos, 4);
  
          crc = wantcrc ^ startxor;
          for (int i = length - 1; i &gt;= fixpos; i--) {
              crc = (crc &lt;&lt; 8) ^ revtable[crc &gt;&gt; (3 * 8)] ^ bytes[i];
          }
  
          Array.Copy(BitConverter.GetBytes(crc), 0, bytes, fixpos, 4);
      }
  
      public Crc32()
      {
          if (Crc32.table == null) {
              uint[] table = new uint[256];
              uint[] revtable = new uint[256];
  
              uint fwd, rev;
              for (int i = 0; i &lt; table.Length; i++) {
                  fwd = (uint)i;
                  rev = (uint)(i) &lt;&lt; (3 * 8);
                  for (int j = 8; j &gt; 0; j--) {
                      if ((fwd &amp; 1) == 1) {
                          fwd = (uint)((fwd &gt;&gt; 1) ^ poly);
                      } else {
                          fwd &gt;&gt;= 1;
                      }
  
                      if ((rev &amp; 0x80000000) != 0) {
                          rev = ((rev ^ poly) &lt;&lt; 1) | 1;
                      } else {
                          rev &lt;&lt;= 1;
                      }
                  }
                  table[i] = fwd;
                  revtable[i] = rev;
              }
  
              Crc32.table = table;
              Crc32.revtable = revtable;
          }
      }
  }
  </code></pre>