CRC-32 with unreversed data and reversed output
Generate a table for a byte-wise 32-bit CRC calculation on the polynomial: x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x^1+x^0. Polynomials over GF(2) are represented in binary, one bit per coefficient, with the lowest powers in the most significant bit. Then adding polynomials is just exclusive-or, and multiplying a polynomial by x is a right shift by one. If we call the above polynomial p, and represent a byte as the polynomial q, also with the lowest power in the most significant bit (so the byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p, where a mod b means the remainder after dividing a by b. This calculation is done using the shift-register method of multiplying and taking the remainder. The register is initialized to zero, and for each incoming bit, x^32 is added mod p to the register if the bit is a one (where x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by x (which is shifting right by one and adding x^32 mod p if the bit shifted out is a one). We start with the highest power (least significant bit) of q and repeat for all eight bits of q. The table is simply the CRC of all possible eight bit values. This is all the information needed to generate CRC's on data a byte at a time for all combinations of CRC register values and incoming bytes.
Inheritance: IChecksum
コード例 #1
0
    public static int Main(string[] args)
    {
        if (args.Length == 0) {
            ShowHelp();
            return 1;
        }

        var parser = new ArgumentParser(args);

        if (!File.Exists(file_)) {
            Console.Error.WriteLine("Cannot find file {0}", file_);
            ShowHelp();
            return 1;
        }

        using (FileStream checksumStream = File.OpenRead(file_)) {

            byte[] buffer = new byte[4096];
            int bytesRead;

            switch (parser.Command) {
                case Command.Help:
                    ShowHelp();
                    break;

                case Command.Crc32:
                    var currentCrc = new Crc32();
                    while ((bytesRead = checksumStream.Read(buffer, 0, buffer.Length)) > 0) {
                        currentCrc.Update(buffer, 0, bytesRead);
                    }
                    Console.WriteLine("CRC32 for {0} is 0x{1:X8}", args[0], currentCrc.Value);
                    break;

                case Command.BZip2:
                    var currentBZip2Crc = new BZip2Crc();
                    while ((bytesRead = checksumStream.Read(buffer, 0, buffer.Length)) > 0) {
                        currentBZip2Crc.Update(buffer, 0, bytesRead);
                    }
                    Console.WriteLine("BZip2CRC32 for {0} is 0x{1:X8}", args[0], currentBZip2Crc.Value);
                    break;

                case Command.Adler:
                    var currentAdler = new Adler32();
                    while ((bytesRead = checksumStream.Read(buffer, 0, buffer.Length)) > 0) {
                        currentAdler.Update(buffer, 0, bytesRead);
                    }
                    Console.WriteLine("Adler32 for {0} is 0x{1:X8}", args[0], currentAdler.Value);
                    break;
            }
        }
        return 0;
    }
コード例 #2
0
ファイル: ChecksumTests.cs プロジェクト: Zo0pZ/SharpZipLib
        public void CRC_32_BZip2()
        {
            var underTestBZip2Crc = new BZip2Crc();
            Assert.AreEqual(0x0, underTestBZip2Crc.Value);

            underTestBZip2Crc.Update(check);
            Assert.AreEqual(0xFC891918, underTestBZip2Crc.Value);

            underTestBZip2Crc.Reset();
            Assert.AreEqual(0x0, underTestBZip2Crc.Value);

            exceptionTesting(underTestBZip2Crc);
        }