📄 crc16.java
字号:
package au.net.aba.crypto.provider;
/*
* $Id: CRC16.java,v 1.7 1998/10/26 01:46:28 leachbj Exp $
* $Author: leachbj $
*
* Copyright (C) 1996-1998 Australian Business Access Pty Ltd.
* All rights reserved.
*
* Use, modification, copying and distribution of this software is subject the
* terms and conditions of the ABA Public Licence. See the file
* "PUBLIC_LICENCE" for additional information.
*
* If you have not received a copy of the Public Licence, you must destroy all
* copies of this file immediately.
*
* $Source: /aba/CVSROOT/jdk1.1/src/au.net.aba/crypto/provider/CRC16.java,v $
* $Revision: 1.7 $
* $Date: 1998/10/26 01:46:28 $
* $State: Exp $
*/
import java.security.MessageDigest;
/**
* A class that implements the CCITT CRC-16 checksumming algorithm, for the
* reflected polynomial 0x8408. Note: if you want to do a zero value check
* on some data and it's associated CRC, the CRC should be added after the
* data, least significant byte first.
*/
public class CRC16 extends MessageDigest
{
public final static String ident = "$Id: CRC16.java,v 1.7 1998/10/26 01:46:28 leachbj Exp $";
//==================================
// Private implementation
//==================================
private short crc16;
/**
* our CRC table
*/
static short[] crcTab;
/**
* generate our lookup table - if we need. Note if we use a
* lookup table this poly is a reflected poly so the table
* lookup should be in the form:
*
* accum = (short)(((accum >> 8) & 0xff)
* ^ crcTab[(accum ^ data) & 0xff]);
*
* where accum is the accumulator for the digest,
* and data is the byte being processed.
*/
static {
crcTab = new short[256];
for (short i = 0; i < 256; i++)
{
crcTab[i] = crcCCITT(i, (short)0);
}
}
//==================================
// Constructors
//==================================
/**
* This constructor is used to begin a new CRC-16 operation
*/
public CRC16()
{
super("CRC16");
engineReset();
}
/**
* Compute a 16 bit CCITT CRC on the fly, we can either
* use this to construct a table by setting the input
* CRC to zero, or accumulate values in the CRC value,
* passing it in each time.
*/
private static short crcCCITT(
short data,
short crc)
{
int s;
s = (data ^ crc) & 0xff;
s = s ^ (s << 4);
s = ((crc >> 8) & 0xff) ^ (s << 8) ^ (s << 3) ^ (s >>> 4);
return (short)s;
}
/**
* return the CRC. Note this resets it.
*
* @return the crc16 as a byte array.
*/
protected synchronized byte[] engineDigest()
{
byte[] digest;
digest = new byte[2];
digest[0] = (byte)(crc16 >>> 8);
digest[1] = (byte)(crc16 & 0xff);
engineReset();
return digest;
}
/*
* MessageDigest methods.
*/
/**
* reset the CRC back to zero.
*/
protected void engineReset()
{
crc16 = 0;
}
/**
* update the CRC with an array of bytes.
*
* @param bytes the input array.
* @param offset the offset to start getting bytes from.
* @param length the number of bytes to be processed.
*/
protected synchronized void engineUpdate(
byte[] bytes,
int offset,
int length)
{
int i;
for (i=0; i < length; i++)
{
crc16 = (short)(((crc16 >> 8) & 0xff)
^ crcTab[(crc16 ^ bytes[offset + i]) & 0xff]);
}
}
/**
* update the CRC with a single byte.
*
* @param b the byte of input.
*/
protected synchronized void engineUpdate(
byte b)
{
crc16 = (short)(((crc16 >> 8) & 0xff)
^ crcTab[(crc16 ^ b) & 0xff]);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -