package com.proxgrind.chameleon.utils.security;
/**
* CRC-8
*
*
*
* | 名称 |
* 多项式 |
* 初始值 |
* 异或值 |
* Bit反转 |
*
*
* | CRC-8 |
* 0x07 |
* 0x00 |
* 0x00 |
* MSB First |
*
*
* | CRC-8/DARC |
* 0x39 |
* 0x00 |
* 0x00 |
* LSB First |
*
*
* | CRC-8/ITU |
* 0x07 |
* 0x00 |
* 0x55 |
* MSB First |
*
*
* | CRC-8/MAXIM |
* 0x31 |
* 0x00 |
* 0x00 |
* LSB First |
*
*
* | CRC-8/ROHC |
* 0x07 |
* 0xFF |
* 0x00 |
* LSB First |
*
*
*
* @author unnamed
*/
public class CRC8Utils {
/**
* CRC-8
*
*
*
* | 多项式 |
* 初始值 |
* 异或值 |
* Bit反转 |
*
*
* | 0x07 |
* 0x00 |
* 0x00 |
* MSB First |
*
*
*
* @param source
* @param offset
* @param length
* @return
*/
public static int CRC8(byte[] source, int offset, int length) {
int wCRCin = 0x00;
int wCPoly = 0x07;
for (int i = offset, cnt = offset + length; i < cnt; i++) {
for (int j = 0; j < 8; j++) {
boolean bit = ((source[i] >> (7 - j) & 1) == 1);
boolean c07 = ((wCRCin >> 7 & 1) == 1);
wCRCin <<= 1;
if (c07 ^ bit)
wCRCin ^= wCPoly;
}
}
wCRCin &= 0xFF;
return wCRCin ^= 0x00;
}
/**
* CRC-8/DARC
*
*
*
* | 多项式 |
* 初始值 |
* 异或值 |
* Bit反转 |
*
*
* | 0x39 |
* 0x00 |
* 0x00 |
* LSB First |
*
*
*
* @param source
* @param offset
* @param length
* @return
*/
public static int CRC8_DARC(byte[] source, int offset, int length) {
int wCRCin = 0x00;
// Integer.reverse(0x39) >>> 24
int wCPoly = 0x9C;
for (int i = offset, cnt = offset + length; i < cnt; i++) {
wCRCin ^= ((long) source[i] & 0xFF);
for (int j = 0; j < 8; j++) {
if ((wCRCin & 0x01) != 0) {
wCRCin >>= 1;
wCRCin ^= wCPoly;
} else {
wCRCin >>= 1;
}
}
}
return wCRCin ^= 0x00;
}
/**
* CRC-8/ITU
*
*
*
* | 多项式 |
* 初始值 |
* 异或值 |
* Bit反转 |
*
*
* | 0x07 |
* 0x00 |
* 0x55 |
* MSB First |
*
*
*
* @param source
* @param offset
* @param length
* @return
*/
public static int CRC8_ITU(byte[] source, int offset, int length) {
int wCRCin = 0x00;
int wCPoly = 0x07;
for (int i = offset, cnt = offset + length; i < cnt; i++) {
for (int j = 0; j < 8; j++) {
boolean bit = ((source[i] >> (7 - j) & 1) == 1);
boolean c07 = ((wCRCin >> 7 & 1) == 1);
wCRCin <<= 1;
if (c07 ^ bit)
wCRCin ^= wCPoly;
}
}
wCRCin &= 0xFF;
return wCRCin ^= 0x55;
}
/**
* CRC-8/MAXIM
*
*
*
* | 多项式 |
* 初始值 |
* 异或值 |
* Bit反转 |
*
*
* | 0x31 |
* 0x00 |
* 0x00 |
* LSB First |
*
*
*
* @param source
* @param offset
* @param length
* @return
*/
public static int CRC8_MAXIM(byte[] source, int offset, int length) {
int wCRCin = 0x00;
// Integer.reverse(0x31) >>> 24
int wCPoly = 0x8C;
for (int i = offset, cnt = offset + length; i < cnt; i++) {
wCRCin ^= ((long) source[i] & 0xFF);
for (int j = 0; j < 8; j++) {
if ((wCRCin & 0x01) != 0) {
wCRCin >>= 1;
wCRCin ^= wCPoly;
} else {
wCRCin >>= 1;
}
}
}
return wCRCin ^= 0x00;
}
/**
* CRC-8/ROHC
*
*
*
* | 多项式 |
* 初始值 |
* 异或值 |
* Bit反转 |
*
*
* | 0x07 |
* 0xFF |
* 0x00 |
* LSB First |
*
*
*
* @param source
* @param offset
* @param length
* @return
*/
public static int CRC8_ROHC(byte[] source, int offset, int length) {
int wCRCin = 0xFF;
// Integer.reverse(0x07) >>> 24
int wCPoly = 0xE0;
for (int i = offset, cnt = offset + length; i < cnt; i++) {
wCRCin ^= ((long) source[i] & 0xFF);
for (int j = 0; j < 8; j++) {
if ((wCRCin & 0x01) != 0) {
wCRCin >>= 1;
wCRCin ^= wCPoly;
} else {
wCRCin >>= 1;
}
}
}
return wCRCin ^= 0x00;
}
}