CRC32校驗算法

public class CRC32
{
/** The crc data checksum so far. */
private uint crc = 0;

    /** The fast CRC table. Computed once when the CRC32 class is loaded. */
    private static uint[] crcTable = makeCrcTable();

    /** Make the table for a fast CRC. */
    private static uint[] makeCrcTable() {
        uint[] crcTable = new uint[256];
        for (int n = 0; n < 256; n++) {
            uint c = (uint)n;
            for (int k = 8; --k >= 0; ) {
                if((c & 1) != 0) c = 0xedb88320 ^ (c >> 1);
                else c = c >> 1;
            }
            crcTable[n] = c;
        }
        return crcTable;
    }

    /**
     * Returns the CRC32 data checksum computed so far.
     */
    public uint getValue() {
        return crc & 0xffffffff;
    }

    /**
     * Resets the CRC32 data checksum as if no update was ever called.
     */
    public void reset() {
        crc = 0;
    }

    /**
     * Adds the complete byte array to the data checksum.
     * 
     * @param buf the buffer which contains the data
     */
    public void update(byte[] buf) {
        uint off = 0;
        int len = buf.Length;
        uint c = ~crc;
        while(--len >= 0) c = crcTable[(c ^ buf[off++]) & 0xff] ^ (c >> 8);
        crc = ~c;
    }

    /**
     * Adds the complete byte array to the data checksum.
     * 
     * @param buf the buffer which contains the data
     */
    public void update(byte[] buf, int off, int len)
    {
        uint c = ~crc;
        while (--len >= 0) c = crcTable[(c ^ buf[off++]) & 0xff] ^ (c >> 8);
        crc = ~c;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,858評論 0 10
  • 高三時,我班的學(xué)霸跟班主任鬧翻了,原因只是因為學(xué)霸說了班主任的目標是欲望。當(dāng)時班主任特別的生氣,罵的很大聲,氣氛也...
    淺淺的箋閱讀 548評論 2 2
  • 5v5 團戰(zhàn) 完勝 我給自己說打完這一架 軍校生活也算是完美了 以前對完美軍旅生涯的定義是 既立過功又背過處分還關(guān)...
    一身橄欖綠閱讀 443評論 2 3
  • 跟著曹師學(xué)中醫(yī) 契要:此篇之由來,吾思之良久,終提筆疾?。 翻開往??師授課(《中醫(yī)藥學(xué)是打開中華?明寶庫的鑰匙》...
    少一先生閱讀 741評論 0 0

友情鏈接更多精彩內(nèi)容