Browse Source

add crc16

Looly 5 years ago
parent
commit
b58f98eb95

+ 21 - 48
hutool-core/src/main/java/cn/hutool/core/io/checksum/CRC16.java

@@ -1,76 +1,49 @@
 package cn.hutool.core.io.checksum;
 
-import java.io.Serializable;
-import java.util.zip.Checksum;
+import cn.hutool.core.io.checksum.crc16.CRC16Checksum;
+import cn.hutool.core.io.checksum.crc16.CRC16IBM;
 
 /**
- * CRC16 循环冗余校验码(Cyclic Redundancy Check)实现<br>
- * 代码来自:https://github.com/BBSc0der
- * 
+ * CRC16 循环冗余校验码(Cyclic Redundancy Check)实现,默认IBM算法
+ *
  * @author looly
  * @since 4.4.1
  */
-public class CRC16 implements Checksum, Serializable {
+public class CRC16 extends CRC16Checksum {
 	private static final long serialVersionUID = 1L;
 
-	// CRC16 = bb3d
-	// Uses irreducible polynomial: 1 + x^2 + x^15 + x^16
+	private final CRC16Checksum crc16;
 
-	private static final int[] TABLE = { //
-			0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241, //
-			0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440, //
-			0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40, //
-			0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841, //
-			0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40, //
-			0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41, //
-			0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641, //
-			0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040, //
-			0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240, //
-			0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441, //
-			0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41, //
-			0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840, //
-			0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41, //
-			0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40, //
-			0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640, //
-			0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041, //
-			0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240, //
-			0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441, //
-			0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41, //
-			0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840, //
-			0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41, //
-			0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40, //
-			0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640, //
-			0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041, //
-			0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241, //
-			0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440, //
-			0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40, //
-			0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841, //
-			0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40, //
-			0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41, //
-			0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641, //
-			0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040//
-	};
+	CRC16(){
+		this(new CRC16IBM());
+	}
 
-	private int sum = 0x0000;
+	/**
+	 * 构造
+	 *
+	 * @param crc16Checksum {@link CRC16Checksum} 实现
+	 */
+	CRC16(CRC16Checksum crc16Checksum){
+		this.crc16 = crc16Checksum;
+	}
 
 	@Override
 	public long getValue() {
-		return sum;
+		return crc16.getValue();
 	}
 
 	@Override
 	public void reset() {
-		sum = 0x0000;
+		crc16.reset();
 	}
 
 	@Override
 	public void update(byte[] b, int off, int len) {
-		for (int i = off; i < off + len; i++)
-			update(b[i]);
+		crc16.update(b, off, len);
 	}
 
 	@Override
 	public void update(int b) {
-		sum = (sum >> 8) ^ TABLE[((sum) ^ (b & 0xff)) & 0xff];
+		crc16.update(b);
 	}
 }

+ 26 - 0
hutool-core/src/main/java/cn/hutool/core/io/checksum/crc16/CRC16CCITT.java

@@ -0,0 +1,26 @@
+package cn.hutool.core.io.checksum.crc16;
+
+/**
+ * CRC16_CCITT:多项式x16+x12+x5+1(0x1021),初始值0x0000,低位在前,高位在后,结果与0x0000异或
+ * 0x8408是0x1021按位颠倒后的结果。
+ *
+ * @author looly
+ * @since 5.3.10
+ */
+public class CRC16CCITT extends CRC16Checksum{
+
+	private static final int wCPoly = 0x8408;
+
+	@Override
+	public void update(int b) {
+		wCRCin ^= (b & 0x00ff);
+		for (int j = 0; j < 8; j++) {
+			if ((wCRCin & 0x0001) != 0) {
+				wCRCin >>= 1;
+				wCRCin ^= wCPoly;
+			} else {
+				wCRCin >>= 1;
+			}
+		}
+	}
+}

+ 33 - 0
hutool-core/src/main/java/cn/hutool/core/io/checksum/crc16/CRC16CCITTFalse.java

@@ -0,0 +1,33 @@
+package cn.hutool.core.io.checksum.crc16;
+
+/**
+ * CRC16_CCITT_FALSE:多项式x16+x12+x5+1(0x1021),初始值0xFFFF,低位在后,高位在前,结果与0x0000异或
+ *
+ * @author looly
+ * @since 5.3.10
+ */
+public class CRC16CCITTFalse extends CRC16Checksum{
+
+	private static final int wCPoly = 0x1021;
+
+	public CRC16CCITTFalse(){
+		this.wCRCin = 0xffff;
+	}
+
+	@Override
+	public void update(byte[] b, int off, int len) {
+		super.update(b, off, len);
+		wCRCin &= 0xffff;
+	}
+
+	@Override
+	public void update(int b) {
+		for (int i = 0; i < 8; i++) {
+			boolean bit = ((b >> (7 - i) & 1) == 1);
+			boolean c15 = ((wCRCin >> 15 & 1) == 1);
+			wCRCin <<= 1;
+			if (c15 ^ bit)
+				wCRCin ^= wCPoly;
+		}
+	}
+}

+ 45 - 0
hutool-core/src/main/java/cn/hutool/core/io/checksum/crc16/CRC16Checksum.java

@@ -0,0 +1,45 @@
+package cn.hutool.core.io.checksum.crc16;
+
+import java.io.Serializable;
+import java.util.zip.Checksum;
+
+/**
+ * CRC16 Checksum,用于提供多种CRC16算法的通用实现<br>
+ * 通过继承此类,重写update和reset完成相应算法。
+ *
+ * @author looly
+ * @since 5.3.10
+ */
+public abstract class CRC16Checksum implements Checksum, Serializable {
+	private static final long serialVersionUID = 1L;
+
+	/**
+	 * CRC16 Checksum 结果值
+	 */
+	protected int wCRCin = 0x0000;
+
+	@Override
+	public long getValue() {
+		return wCRCin;
+	}
+
+	@Override
+	public void reset() {
+		wCRCin = 0x0000;
+	}
+
+	/**
+	 * 计算全部字节
+	 * @param b 字节
+	 */
+	public void update(byte[] b){
+		update(b, 0, b.length);
+	}
+
+	@Override
+	public void update(byte[] b, int off, int len) {
+		for (int i = off; i < off + len; i++)
+			update(b[i]);
+	}
+
+}

+ 32 - 0
hutool-core/src/main/java/cn/hutool/core/io/checksum/crc16/CRC16DNP.java

@@ -0,0 +1,32 @@
+package cn.hutool.core.io.checksum.crc16;
+
+/**
+ * CRC16_DNP:多项式x16+x13+x12+x11+x10+x8+x6+x5+x2+1(0x3D65),初始值0x0000,低位在前,高位在后,结果与0xFFFF异或
+ * 0xA6BC是0x3D65按位颠倒后的结果
+ *
+ * @author looly
+ * @since 5.3.10
+ */
+public class CRC16DNP extends CRC16Checksum{
+
+	private static final int wCPoly = 0xA6BC;
+
+	@Override
+	public void update(byte[] b, int off, int len) {
+		super.update(b, off, len);
+		wCRCin ^= 0xffff;
+	}
+
+	@Override
+	public void update(int b) {
+		wCRCin ^= (b & 0x00ff);
+		for (int j = 0; j < 8; j++) {
+			if ((wCRCin & 0x0001) != 0) {
+				wCRCin >>= 1;
+				wCRCin ^= wCPoly;
+			} else {
+				wCRCin >>= 1;
+			}
+		}
+	}
+}

+ 26 - 0
hutool-core/src/main/java/cn/hutool/core/io/checksum/crc16/CRC16IBM.java

@@ -0,0 +1,26 @@
+package cn.hutool.core.io.checksum.crc16;
+
+/**
+ * CRC16_IBM:多项式x16+x15+x2+1(0x8005),初始值0x0000,低位在前,高位在后,结果与0x0000异或
+ * 0xA001是0x8005按位颠倒后的结果
+ *
+ * @author looly
+ * @since 5.3.10
+ */
+public class CRC16IBM extends CRC16Checksum{
+
+	private static final int wCPoly = 0xa001;
+
+	@Override
+	public void update(int b) {
+		wCRCin ^= (b & 0x00ff);
+		for (int j = 0; j < 8; j++) {
+			if ((wCRCin & 0x0001) != 0) {
+				wCRCin >>= 1;
+				wCRCin ^= wCPoly;
+			} else {
+				wCRCin >>= 1;
+			}
+		}
+	}
+}

+ 32 - 0
hutool-core/src/main/java/cn/hutool/core/io/checksum/crc16/CRC16Maxim.java

@@ -0,0 +1,32 @@
+package cn.hutool.core.io.checksum.crc16;
+
+/**
+ * CRC16_MAXIM:多项式x16+x15+x2+1(0x8005),初始值0x0000,低位在前,高位在后,结果与0xFFFF异或
+ * 0xA001是0x8005按位颠倒后的结果
+ *
+ * @author looly
+ * @since 5.3.10
+ */
+public class CRC16Maxim extends CRC16Checksum{
+
+	private static final int wCPoly = 0xa001;
+
+	@Override
+	public void update(byte[] b, int off, int len) {
+		super.update(b, off, len);
+		wCRCin ^= 0xffff;
+	}
+
+	@Override
+	public void update(int b) {
+		wCRCin ^= (b & 0x00ff);
+		for (int j = 0; j < 8; j++) {
+			if ((wCRCin & 0x0001) != 0) {
+				wCRCin >>= 1;
+				wCRCin ^= wCPoly;
+			} else {
+				wCRCin >>= 1;
+			}
+		}
+	}
+}

+ 31 - 0
hutool-core/src/main/java/cn/hutool/core/io/checksum/crc16/CRC16Modbus.java

@@ -0,0 +1,31 @@
+package cn.hutool.core.io.checksum.crc16;
+
+/**
+ * CRC-16 (Modbus)
+ * CRC16_MODBUS:多项式x16+x15+x2+1(0x8005),初始值0xFFFF,低位在前,高位在后,结果与0x0000异或
+ * 0xA001是0x8005按位颠倒后的结果
+ *
+ * @author looly
+ * @since 5.3.10
+ */
+public class CRC16Modbus extends CRC16Checksum{
+
+	private static final int wCPoly = 0xa001;
+
+	public CRC16Modbus(){
+		this.wCRCin = 0xffff;
+	}
+
+	@Override
+	public void update(int b) {
+		wCRCin ^= ((int) b & 0x00ff);
+		for (int j = 0; j < 8; j++) {
+			if ((wCRCin & 0x0001) != 0) {
+				wCRCin >>= 1;
+				wCRCin ^= wCPoly;
+			} else {
+				wCRCin >>= 1;
+			}
+		}
+	}
+}

+ 36 - 0
hutool-core/src/main/java/cn/hutool/core/io/checksum/crc16/CRC16USB.java

@@ -0,0 +1,36 @@
+package cn.hutool.core.io.checksum.crc16;
+
+/**
+ * CRC16_USB:多项式x16+x15+x2+1(0x8005),初始值0xFFFF,低位在前,高位在后,结果与0xFFFF异或
+ * 0xA001是0x8005按位颠倒后的结果
+ *
+ * @author looly
+ * @since 5.3.10
+ */
+public class CRC16USB extends CRC16Checksum{
+
+	private static final int wCPoly = 0xa001;
+
+	public CRC16USB(){
+		this.wCRCin = 0xFFFF;
+	}
+
+	@Override
+	public void update(byte[] b, int off, int len) {
+		super.update(b, off, len);
+		wCRCin ^= 0xffff;
+	}
+
+	@Override
+	public void update(int b) {
+		wCRCin ^= (b & 0x00ff);
+		for (int j = 0; j < 8; j++) {
+			if ((wCRCin & 0x0001) != 0) {
+				wCRCin >>= 1;
+				wCRCin ^= wCPoly;
+			} else {
+				wCRCin >>= 1;
+			}
+		}
+	}
+}

+ 36 - 0
hutool-core/src/main/java/cn/hutool/core/io/checksum/crc16/CRC16X25.java

@@ -0,0 +1,36 @@
+package cn.hutool.core.io.checksum.crc16;
+
+/**
+ * CRC16_X25:多项式x16+x12+x5+1(0x1021),初始值0xffff,低位在前,高位在后,结果与0xFFFF异或
+ * 0x8408是0x1021按位颠倒后的结果。
+ *
+ * @author looly
+ * @since 5.3.10
+ */
+public class CRC16X25 extends CRC16Checksum{
+
+	private static final int wCPoly = 0x8408;
+
+	public CRC16X25(){
+		this.wCRCin = 0xffff;
+	}
+
+	@Override
+	public void update(byte[] b, int off, int len) {
+		super.update(b, off, len);
+		wCRCin ^= 0xffff;
+	}
+
+	@Override
+	public void update(int b) {
+		wCRCin ^= (b & 0x00ff);
+		for (int j = 0; j < 8; j++) {
+			if ((wCRCin & 0x0001) != 0) {
+				wCRCin >>= 1;
+				wCRCin ^= wCPoly;
+			} else {
+				wCRCin >>= 1;
+			}
+		}
+	}
+}

+ 31 - 0
hutool-core/src/main/java/cn/hutool/core/io/checksum/crc16/CRC16XModem.java

@@ -0,0 +1,31 @@
+package cn.hutool.core.io.checksum.crc16;
+
+/**
+ * CRC-CCITT (XModem)
+ * CRC16_XMODEM:多项式x16+x12+x5+1(0x1021),初始值0x0000,低位在后,高位在前,结果与0x0000异或
+ *
+ * @author looly
+ * @since 5.3.10
+ */
+public class CRC16XModem extends CRC16Checksum{
+
+	// 0001 0000 0010 0001 (0, 5, 12)
+	private static final int wCPoly = 0x1021;
+
+	@Override
+	public void update(byte[] b, int off, int len) {
+		super.update(b, off, len);
+		wCRCin &= 0xffff;
+	}
+
+	@Override
+	public void update(int b) {
+		for (int i = 0; i < 8; i++) {
+			boolean bit = ((b >> (7 - i) & 1) == 1);
+			boolean c15 = ((wCRCin >> 15 & 1) == 1);
+			wCRCin <<= 1;
+			if (c15 ^ bit)
+				wCRCin ^= wCPoly;
+		}
+	}
+}

+ 7 - 0
hutool-core/src/main/java/cn/hutool/core/io/checksum/crc16/package-info.java

@@ -0,0 +1,7 @@
+/**
+ * CRC16相关算法封装为Checksum
+ * 
+ * @author looly
+ *
+ */
+package cn.hutool.core.io.checksum.crc16;

+ 82 - 0
hutool-core/src/test/java/cn/hutool/core/io/checksum/CRC16Test.java

@@ -0,0 +1,82 @@
+package cn.hutool.core.io.checksum;
+
+import cn.hutool.core.io.checksum.crc16.CRC16CCITT;
+import cn.hutool.core.io.checksum.crc16.CRC16CCITTFalse;
+import cn.hutool.core.io.checksum.crc16.CRC16DNP;
+import cn.hutool.core.io.checksum.crc16.CRC16IBM;
+import cn.hutool.core.io.checksum.crc16.CRC16Maxim;
+import cn.hutool.core.io.checksum.crc16.CRC16Modbus;
+import cn.hutool.core.io.checksum.crc16.CRC16USB;
+import cn.hutool.core.io.checksum.crc16.CRC16X25;
+import cn.hutool.core.io.checksum.crc16.CRC16XModem;
+import cn.hutool.core.util.HexUtil;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class CRC16Test {
+
+	private final String data = "QN=20160801085857223;ST=23;CN=2011;PW=123456;MN=010000A8900016F000169DC0;Flag=5;CP=&&DataTime=20160801085857; LA-Rtd=50.1&&";
+
+	@Test
+	public void ccittTest(){
+		final CRC16CCITT crc16 = new CRC16CCITT();
+		crc16.update(data.getBytes());
+		Assert.assertEquals("c852", HexUtil.toHex(crc16.getValue()));
+	}
+
+	@Test
+	public void ccittFalseTest(){
+		final CRC16CCITTFalse crc16 = new CRC16CCITTFalse();
+		crc16.update(data.getBytes());
+		Assert.assertEquals("a5e4", HexUtil.toHex(crc16.getValue()));
+	}
+
+	@Test
+	public void xmodemTest(){
+		final CRC16XModem crc16 = new CRC16XModem();
+		crc16.update(data.getBytes());
+		Assert.assertEquals("5a8d", HexUtil.toHex(crc16.getValue()));
+	}
+
+	@Test
+	public void x25Test(){
+		final CRC16X25 crc16 = new CRC16X25();
+		crc16.update(data.getBytes());
+		Assert.assertEquals("a152", HexUtil.toHex(crc16.getValue()));
+	}
+
+	@Test
+	public void modbusTest(){
+		final CRC16Modbus crc16 = new CRC16Modbus();
+		crc16.update(data.getBytes());
+		Assert.assertEquals("25fb", HexUtil.toHex(crc16.getValue()));
+	}
+
+	@Test
+	public void ibmTest(){
+		final CRC16IBM crc16 = new CRC16IBM();
+		crc16.update(data.getBytes());
+		Assert.assertEquals("18c", HexUtil.toHex(crc16.getValue()));
+	}
+
+	@Test
+	public void maximTest(){
+		final CRC16Maxim crc16 = new CRC16Maxim();
+		crc16.update(data.getBytes());
+		Assert.assertEquals("fe73", HexUtil.toHex(crc16.getValue()));
+	}
+
+	@Test
+	public void usbTest(){
+		final CRC16USB crc16 = new CRC16USB();
+		crc16.update(data.getBytes());
+		Assert.assertEquals("da04", HexUtil.toHex(crc16.getValue()));
+	}
+
+	@Test
+	public void dnpTest(){
+		final CRC16DNP crc16 = new CRC16DNP();
+		crc16.update(data.getBytes());
+		Assert.assertEquals("3d1a", HexUtil.toHex(crc16.getValue()));
+	}
+}