JAVA CRC16

简介: JAVA CRC16

JAVA CRC16

/**
 * CRC-16
 * 
 * <table width="400px" border="1" cellpadding="0" cellspacing="0">
 *     <tr>
 *         <th>名称</th>
 *         <th>多项式</th>
 *         <th>初始值</th>
 *         <th>异或值</th>
 *         <th>Bit反转</th>
 *     </tr>
 *     <tr>
 *         <td>&nbsp; CRC-16/IBM</td>
 *         <td align="center">0x8005</td>
 *         <td align="center">0x0000</td>
 *         <td align="center">0x0000</td>
 *         <td align="center">LSB First</td>
 *     </tr>
 *     <tr>
 *         <td>&nbsp; CRC-16/CCITT</td>
 *         <td align="center">0x1021</td>
 *         <td align="center">0x0000</td>
 *         <td align="center">0x0000</td>
 *         <td align="center">LSB First</td>
 *     </tr>
 *     <tr>
 *         <td>&nbsp; CRC-16/CCITT-FALSE</td>
 *         <td align="center">0x1021</td>
 *         <td align="center">0xFFFF</td>
 *         <td align="center">0x0000</td>
 *         <td align="center">MSB First</td>
 *     </tr>
 *     <tr>
 *         <td>&nbsp; CRC-16/DECT R</td>
 *         <td align="center">0x0589</td>
 *         <td align="center">0x0000</td>
 *         <td align="center">0x0001</td>
 *         <td align="center">MSB First</td>
 *     </tr>
 *     <tr>
 *         <td>&nbsp; CRC-16/DECT X</td>
 *         <td align="center">0x0589</td>
 *         <td align="center">0x0000</td>
 *         <td align="center">0x0000</td>
 *         <td align="center">MSB First</td>
 *     </tr>
 *     <tr>
 *         <td>&nbsp; CRC-16/DNP</td>
 *         <td align="center">0x3D65</td>
 *         <td align="center">0x0000</td>
 *         <td align="center">0xFFFF</td>
 *         <td align="center">LSB First</td>
 *     </tr>
 *     <tr>
 *         <td>&nbsp; CRC-16/GENIBUS</td>
 *         <td align="center">0x1021</td>
 *         <td align="center">0xFFFF</td>
 *         <td align="center">0xFFFF</td>
 *         <td align="center">MSB First</td>
 *     </tr>
 *     <tr>
 *         <td>&nbsp; CRC-16/MAXIM</td>
 *         <td align="center">0x8005</td>
 *         <td align="center">0x0000</td>
 *         <td align="center">0xFFFF</td>
 *         <td align="center">LSB First</td>
 *     </tr>
 *     <tr>
 *         <td>&nbsp; CRC-16/MODBUS</td>
 *         <td align="center">0x8005</td>
 *         <td align="center">0xFFFF</td>
 *         <td align="center">0x0000</td>
 *         <td align="center">LSB First</td>
 *     </tr>
 *     <tr>
 *         <td>&nbsp; CRC-16/USB</td>
 *         <td align="center">0x8005</td>
 *         <td align="center">0xFFFF</td>
 *         <td align="center">0xFFFF</td>
 *         <td align="center">LSB First</td>
 *     </tr>
 *     <tr>
 *         <td>&nbsp; CRC-16/X25</td>
 *         <td align="center">0x1021</td>
 *         <td align="center">0xFFFF</td>
 *         <td align="center">0xFFFF</td>
 *         <td align="center">LSB First</td>
 *     </tr>
 *     <tr>
 *         <td>&nbsp; CRC-16/XMODEM</td>
 *         <td align="center">0x1021</td>
 *         <td align="center">0x0000</td>
 *         <td align="center">0x0000</td>
 *         <td align="center">MSB First</td>
 *     </tr>
 * </table>
 * 
 * @author unnamed
 *
*/
public class CRC16Utils {
    /**
     * CRC-16/IBM
     * 
     * <table width="400px" border="1" cellpadding="0" cellspacing="0">
     *     <tr>
     *         <th>多项式</th>
     *         <th>初始值</th>
     *         <th>异或值</th>
     *         <th>Bit反转</th>
     *     </tr>
     *     <tr>
     *         <td align="center">0x8005</td>
     *         <td align="center">0x0000</td>
     *         <td align="center">0x0000</td>
     *         <td align="center">LSB First</td>
     *     </tr>
     * </table>
     * 
     * @param  source
     * @param  offset
     * @param  length
     * @return
     */
    public static int CRC16_IBM(byte[] source, int offset, int length) {
        int wCRCin = 0x0000;
        // Integer.reverse(0x8005) >>> 16
        int wCPoly = 0xA001;
        for (int i = offset, cnt = offset + length; i < cnt; i++) {
            wCRCin ^= ((int) source[i] & 0x00FF);
            for (int j = 0; j < 8; j++) {
                if ((wCRCin & 0x0001) != 0) {
                    wCRCin >>= 1;
                    wCRCin ^= wCPoly;
                } else {
                    wCRCin >>= 1;
                }
            }
        }
        return wCRCin ^= 0x0000;
    }
    /**
     * CRC-16/CCITT
     * 
     * <table width="400px" border="1" cellpadding="0" cellspacing="0">
     *     <tr>
     *         <th>多项式</th>
     *         <th>初始值</th>
     *         <th>异或值</th>
     *         <th>Bit反转</th>
     *     </tr>
     *     <tr>
     *         <td align="center">0x1021</td>
     *         <td align="center">0x0000</td>
     *         <td align="center">0x0000</td>
     *         <td align="center">LSB First</td>
     *     </tr>
     * </table>
     * 
     * @param  source
     * @param  offset
     * @param  length
     * @return
     */
    public static int CRC16_CCITT(byte[] source, int offset, int length) {
        int wCRCin = 0x0000;
        // Integer.reverse(0x1021) >>> 16
        int wCPoly = 0x8408;
        for (int i = offset, cnt = offset + length; i < cnt; i++) {
            wCRCin ^= ((int) source[i] & 0x00FF);
            for (int j = 0; j < 8; j++) {
                if ((wCRCin & 0x0001) != 0) {
                    wCRCin >>= 1;
                    wCRCin ^= wCPoly;
                } else {
                    wCRCin >>= 1;
                }
            }
        }
        return wCRCin ^= 0x0000;
    }
    /**
     * CRC-16/CCITT-FALSE
     * 
     * <table width="400px" border="1" cellpadding="0" cellspacing="0">
     *     <tr>
     *         <th>多项式</th>
     *         <th>初始值</th>
     *         <th>异或值</th>
     *         <th>Bit反转</th>
     *     </tr>
     *     <tr>
     *         <td align="center">0x1021</td>
     *         <td align="center">0xFFFF</td>
     *         <td align="center">0x0000</td>
     *         <td align="center">MSB First</td>
     *     </tr>
     * </table>
     * 
     * @param  source
     * @param  offset
     * @param  length
     * @return
     */
    public static int CRC16_CCITT_FALSE(byte[] source, int offset, int length) {
        int wCRCin = 0xFFFF;
        int wCPoly = 0x1021;
        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 c15 = ((wCRCin >> 15 & 1) == 1);
                wCRCin <<= 1;
                if (c15 ^ bit)
                    wCRCin ^= wCPoly;
            }
        }
        wCRCin &= 0xFFFF;
        return wCRCin ^= 0x0000;
    }
    /**
     * CRC-16/DECT R
     * 
     * <table width="400px" border="1" cellpadding="0" cellspacing="0">
     *     <tr>
     *         <th>多项式</th>
     *         <th>初始值</th>
     *         <th>异或值</th>
     *         <th>Bit反转</th>
     *     </tr>
     *     <tr>
     *         <td align="center">0x0589</td>
     *         <td align="center">0x0000</td>
     *         <td align="center">0x0001</td>
     *         <td align="center">MSB First</td>
     *     </tr>
     * </table>
     * 
     * @param  source
     * @param  offset
     * @param  length
     * @return
     */
    public static int CRC16_DECT_R(byte[] source, int offset, int length) {
        int wCRCin = 0x0000;
        int wCPoly = 0x0589;
        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 c15 = ((wCRCin >> 15 & 1) == 1);
                wCRCin <<= 1;
                if (c15 ^ bit)
                    wCRCin ^= wCPoly;
            }
        }
        wCRCin &= 0xFFFF;
        return wCRCin ^= 0x0001;
    }
    /**
     * CRC-16/DECT X
     * 
     * <table width="400px" border="1" cellpadding="0" cellspacing="0">
     *     <tr>
     *         <th>多项式</th>
     *         <th>初始值</th>
     *         <th>异或值</th>
     *         <th>Bit反转</th>
     *     </tr>
     *     <tr>
     *         <td align="center">0x0589</td>
     *         <td align="center">0x0000</td>
     *         <td align="center">0x0000</td>
     *         <td align="center">MSB First</td>
     *     </tr>
     * </table>
     * 
     * @param  source
     * @param  offset
     * @param  length
     * @return
     */
    public static int CRC16_DECT_X(byte[] source, int offset, int length) {
        int wCRCin = 0x0000;
        int wCPoly = 0x0589;
        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 c15 = ((wCRCin >> 15 & 1) == 1);
                wCRCin <<= 1;
                if (c15 ^ bit)
                    wCRCin ^= wCPoly;
            }
        }
        wCRCin &= 0xFFFF;
        return wCRCin ^= 0x0000;
    }
    /**
     * CRC-16/DNP
     * 
     * <table width="400px" border="1" cellpadding="0" cellspacing="0">
     *     <tr>
     *         <th>多项式</th>
     *         <th>初始值</th>
     *         <th>异或值</th>
     *         <th>Bit反转</th>
     *     </tr>
     *     <tr>
     *         <td align="center">0x3D65</td>
     *         <td align="center">0x0000</td>
     *         <td align="center">0xFFFF</td>
     *         <td align="center">LSB First</td>
     *     </tr>
     * </table>
     * 
     * @param  source
     * @param  offset
     * @param  length
     * @return
     */
    public static int CRC16_DNP(byte[] source, int offset, int length) {
        int wCRCin = 0x0000;
        // Integer.reverse(0x3D65) >>> 16
        int wCPoly = 0xA6BC;
        for (int i = offset, cnt = offset + length; i < cnt; i++) {
            wCRCin ^= ((int) source[i] & 0x00FF);
            for (int j = 0; j < 8; j++) {
                if ((wCRCin & 0x0001) != 0) {
                    wCRCin >>= 1;
                    wCRCin ^= wCPoly;
                } else {
                    wCRCin >>= 1;
                }
            }
        }
        return wCRCin ^= 0xFFFF;
    }
    /**
     * CRC-16/GENIBUS
     * 
     * <table width="400px" border="1" cellpadding="0" cellspacing="0">
     *     <tr>
     *         <th>多项式</th>
     *         <th>初始值</th>
     *         <th>异或值</th>
     *         <th>Bit反转</th>
     *     </tr>
     *     <tr>
     *         <td align="center">0x1021</td>
     *         <td align="center">0xFFFF</td>
     *         <td align="center">0xFFFF</td>
     *         <td align="center">MSB First</td>
     *     </tr>
     * </table>
     * 
     * @param  source
     * @param  offset
     * @param  length
     * @return
     */
    public static int CRC16_GENIBUS(byte[] source, int offset, int length) {
        int wCRCin = 0xFFFF;
        int wCPoly = 0x1021;
        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 c15 = ((wCRCin >> 15 & 1) == 1);
                wCRCin <<= 1;
                if (c15 ^ bit)
                    wCRCin ^= wCPoly;
            }
        }
        wCRCin &= 0xFFFF;
        return wCRCin ^= 0xFFFF;
    }
    /**
     * CRC-16/MAXIM
     * 
     * <table width="400px" border="1" cellpadding="0" cellspacing="0">
     *     <tr>
     *         <th>多项式</th>
     *         <th>初始值</th>
     *         <th>异或值</th>
     *         <th>Bit反转</th>
     *     </tr>
     *     <tr>
     *         <td align="center">0x8005</td>
     *         <td align="center">0x0000</td>
     *         <td align="center">0xFFFF</td>
     *         <td align="center">LSB First</td>
     *     </tr>
     * </table>
     * 
     * @param  source
     * @param  offset
     * @param  length
     * @return
     */
    public static int CRC16_MAXIM(byte[] source, int offset, int length) {
        int wCRCin = 0x0000;
        // Integer.reverse(0x8005) >>> 16
        int wCPoly = 0xA001;
        for (int i = offset, cnt = offset + length; i < cnt; i++) {
            wCRCin ^= ((int) source[i] & 0x00FF);
            for (int j = 0; j < 8; j++) {
                if ((wCRCin & 0x0001) != 0) {
                    wCRCin >>= 1;
                    wCRCin ^= wCPoly;
                } else {
                    wCRCin >>= 1;
                }
            }
        }
        return wCRCin ^= 0xFFFF;
    }
    /**
     * CRC-16/MODBUS
     * 
     * <table width="400px" border="1" cellpadding="0" cellspacing="0">
     *     <tr>
     *         <th>多项式</th>
     *         <th>初始值</th>
     *         <th>异或值</th>
     *         <th>Bit反转</th>
     *     </tr>
     *     <tr>
     *         <td align="center">0x8005</td>
     *         <td align="center">0xFFFF</td>
     *         <td align="center">0x0000</td>
     *         <td align="center">LSB First</td>
     *     </tr>
     * </table>
     * 
     * @param  source
     * @param  offset
     * @param  length
     * @return
     */
    public static int CRC16_MODBUS(byte[] source, int offset, int length) {
        int wCRCin = 0xFFFF;
        // Integer.reverse(0x8005) >>> 16
        int wCPoly = 0xA001;
        for (int i = offset, cnt = offset + length; i < cnt; i++) {
            wCRCin ^= ((int) source[i] & 0x00FF);
            for (int j = 0; j < 8; j++) {
                if ((wCRCin & 0x0001) != 0) {
                    wCRCin >>= 1;
                    wCRCin ^= wCPoly;
                } else {
                    wCRCin >>= 1;
                }
            }
        }
        return wCRCin ^= 0x0000;
    }
    /**
     * CRC-16/USB
     * 
     * <table width="400px" border="1" cellpadding="0" cellspacing="0">
     *     <tr>
     *         <th>多项式</th>
     *         <th>初始值</th>
     *         <th>异或值</th>
     *         <th>Bit反转</th>
     *     </tr>
     *     <tr>
     *         <td align="center">0x8005</td>
     *         <td align="center">0xFFFF</td>
     *         <td align="center">0xFFFF</td>
     *         <td align="center">LSB First</td>
     *     </tr>
     * </table>
     * 
     * @param  source
     * @param  offset
     * @param  length
     * @return
     */
    public static int CRC16_USB(byte[] source, int offset, int length) {
        int wCRCin = 0xFFFF;
        // Integer.reverse(0x8005) >>> 16
        int wCPoly = 0xA001;
        for (int i = offset, cnt = offset + length; i < cnt; i++) {
            wCRCin ^= ((int) source[i] & 0x00FF);
            for (int j = 0; j < 8; j++) {
                if ((wCRCin & 0x0001) != 0) {
                    wCRCin >>= 1;
                    wCRCin ^= wCPoly;
                } else {
                    wCRCin >>= 1;
                }
            }
        }
        return wCRCin ^= 0xFFFF;
    }
    /**
     * CRC-16/X25
     * 
     * <table width="400px" border="1" cellpadding="0" cellspacing="0">
     *     <tr>
     *         <th>多项式</th>
     *         <th>初始值</th>
     *         <th>异或值</th>
     *         <th>Bit反转</th>
     *     </tr>
     *     <tr>
     *         <td align="center">0x1021</td>
     *         <td align="center">0xFFFF</td>
     *         <td align="center">0xFFFF</td>
     *         <td align="center">LSB First</td>
     *     </tr>
     * </table>
     * 
     * @param  source
     * @param  offset
     * @param  length
     * @return
     */
    public static int CRC16_X25(byte[] source, int offset, int length) {
        int wCRCin = 0xFFFF;
        // Integer.reverse(0x1021) >>> 16
        int wCPoly = 0x8408;
        for (int i = offset, cnt = offset + length; i < cnt; i++) {
            wCRCin ^= ((int) source[i] & 0x00FF);
            for (int j = 0; j < 8; j++) {
                if ((wCRCin & 0x0001) != 0) {
                    wCRCin >>= 1;
                    wCRCin ^= wCPoly;
                } else {
                    wCRCin >>= 1;
                }
            }
        }
        return wCRCin ^= 0xFFFF;
    }
    /**
     * CRC-16/XMODEM
     * 
     * <table width="400px" border="1" cellpadding="0" cellspacing="0">
     *     <tr>
     *         <th>多项式</th>
     *         <th>初始值</th>
     *         <th>异或值</th>
     *         <th>Bit反转</th>
     *     </tr>
     *     <tr>
     *         <td align="center">0x1021</td>
     *         <td align="center">0x0000</td>
     *         <td align="center">0x0000</td>
     *         <td align="center">MSB First</td>
     *     </tr>
     * </table>
     * 
     * @param  source
     * @param  offset
     * @param  length
     * @return
     */
    public static int CRC16_XMODEM(byte[] source, int offset, int length) {
        int wCRCin = 0x0000;
        int wCPoly = 0x1021;
        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 c15 = ((wCRCin >> 15 & 1) == 1);
                wCRCin <<= 1;
                if (c15 ^ bit)
                    wCRCin ^= wCPoly;
            }
        }
        wCRCin &= 0xFFFF;
        return wCRCin ^= 0x0000;
    }
}

转:https://blog.csdn.net/zte1055889498/article/details/102686777

目录
相关文章
|
Java Apache
JAVA文件的MD5获取方法
JAVA文件的MD5获取方法
955 0
|
5月前
|
Java
JAVA CRC8
JAVA CRC8
44 0
|
5月前
|
Java
JAVA CRC32
JAVA CRC32
29 0
|
存储 Java 计算机视觉
java 之byte
当涉及到处理数据时,Java 提供了多种数据类型,其中包括 `byte` 类型。在本文中,我们将深入探讨 Java 中的 `byte` 数据类型,了解它的特点、用途以及在编程中的实际应用。
|
Java
Java获取文件的MD5
Java获取文件的MD5
199 0
|
存储 算法 Java
JAVA中的hash
一.hashcode()方法 首先hashcode()方法是万类始祖Object自带的一个方法,翻看源码可以知道是一个native方法,也就是说是调用的操作系统底层函数生成的一个值——hashcode。hashCode是jdk根据对象的地址或者字符串或者数字算出来的int类型的数值。
146 0
|
Java API 数据安全/隐私保护
【小家java】java8新特性之---Base64加密和解密原理
【小家java】java8新特性之---Base64加密和解密原理
|
算法 安全 Java
Java 中的随机数 | Java Debug 笔记
Java 中的随机数 | Java Debug 笔记
|
算法 安全 数据处理
Java-实现文件sha1编码获取
SHA-1是一种密码散列函数,美国国家安全局设计,并由美国国家标准技术研究所(NIST)发布为联邦数据处理标准(FIPS)。2005年,密码分析人员发现了对SHA-1的有效攻击方法,这表明该算法可能不够安全。
779 0
|
算法 网络安全 数据安全/隐私保护
Java-实现文件的MD5编码获取
MD5信息摘要算法(英语:MD5 Message-Digest Algorithm),一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于确保信息传输完整一致。这套算法的程序在 RFC 1321 标准中被加以规范。1996年后该算法被证实存在弱点,可以被加以破解,对于需要高度安全性的数据,专家一般建议改用其他算法,如SHA-2。2004年,证实MD5算法无法防止碰撞(collision),因此不适用于安全性认证,如SSL公开密钥认证或是数字签名等用途。
394 0