名称 | 多项式 | 初始值 | 异或值 | Bit反转 |
---|---|---|---|---|
CRC-32 | 0x04C11DB7 | 0xFFFFFFFF | 0xFFFFFFFF | LSB First |
CRC-32/B | 0x04C11DB7 | 0xFFFFFFFF | 0xFFFFFFFF | MSB First |
CRC-32/C | 0x1EDC6F41 | 0xFFFFFFFF | 0xFFFFFFFF | LSB First |
CRC-32/D | 0xA833982B | 0xFFFFFFFF | 0xFFFFFFFF | LSB First |
CRC-32/MPEG-2 | 0x04C11DB7 | 0xFFFFFFFF | 0x00000000 | MSB First |
CRC-32/POSIX | 0x04C11DB7 | 0x00000000 | 0xFFFFFFFF | MSB First |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
public class CRC32Utils { /** * CRC-32 * * @param source * @param offset * @param length * @return */ public static long CRC32(byte[] source, int offset, int length) { long wCRCin = 0xFFFFFFFFL; // Long.reverse(0x04C11DB7L) >>> 32 long wCPoly = 0xEDB88320L; for (int i = offset, cnt = offset + length; i < cnt; i++) { wCRCin ^= ((long) source[i] & 0x000000FFL); for (int j = 0; j < 8; j++) { if ((wCRCin & 0x00000001L) != 0) { wCRCin >>= 1; wCRCin ^= wCPoly; } else { wCRCin >>= 1; } } } return wCRCin ^= 0xFFFFFFFFL; } /** * CRC-32/B * * @param source * @param offset * @param length * @return */ public static long CRC32_B(byte[] source, int offset, int length) { long wCRCin = 0xFFFFFFFFL; long wCPoly = 0x04C11DB7L; 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 c31 = ((wCRCin >> 31 & 1) == 1); wCRCin <<= 1; if (c31 ^ bit) { wCRCin ^= wCPoly; } } } wCRCin &= 0xFFFFFFFFL; return wCRCin ^= 0xFFFFFFFFL; } /** * CRC-32/C * * @param source * @param offset * @param length * @return */ public static long CRC32_C(byte[] source, int offset, int length) { long wCRCin = 0xFFFFFFFFL; // Long.reverse(0x1EDC6F41L) >>> 32 long wCPoly = 0x82F63B78L; for (int i = offset, cnt = offset + length; i < cnt; i++) { wCRCin ^= ((long) source[i] & 0x000000FFL); for (int j = 0; j < 8; j++) { if ((wCRCin & 0x00000001L) != 0) { wCRCin >>= 1; wCRCin ^= wCPoly; } else { wCRCin >>= 1; } } } return wCRCin ^= 0xFFFFFFFFL; } /** * CRC-32/D * * @param source * @param offset * @param length * @return */ public static long CRC32_D(byte[] source, int offset, int length) { long wCRCin = 0xFFFFFFFFL; // Long.reverse(0xA833982BL) >>> 32 long wCPoly = 0xD419CC15L; for (int i = offset, cnt = offset + length; i < cnt; i++) { wCRCin ^= ((long) source[i] & 0x000000FFL); for (int j = 0; j < 8; j++) { if ((wCRCin & 0x00000001L) != 0) { wCRCin >>= 1; wCRCin ^= wCPoly; } else { wCRCin >>= 1; } } } return wCRCin ^= 0xFFFFFFFFL; } /** * CRC-32/MPEG-2 * * @param source * @param offset * @param length * @return */ public static long CRC32_MPEG_2(byte[] source, int offset, int length) { long wCRCin = 0xFFFFFFFFL; long wCPoly = 0x04C11DB7L; 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 c31 = ((wCRCin >> 31 & 1) == 1); wCRCin <<= 1; if (c31 ^ bit) { wCRCin ^= wCPoly; } } } wCRCin &= 0xFFFFFFFFL; return wCRCin ^= 0x00000000L; } /** * CRC-32/POSIX * * @param source * @param offset * @param length * @return */ public static long CRC32_POSIX(byte[] source, int offset, int length) { long wCRCin = 0x00000000L; long wCPoly = 0x04C11DB7L; 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 c31 = ((wCRCin >> 31 & 1) == 1); wCRCin <<= 1; if (c31 ^ bit) { wCRCin ^= wCPoly; } } } wCRCin &= 0xFFFFFFFFL; return wCRCin ^= 0xFFFFFFFFL; } } |