c 语言 crc32校验算法,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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值