forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CRC32.java
31 lines (25 loc) · 861 Bytes
/
CRC32.java
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
package Others;
import java.util.BitSet;
/**
* Generates a crc32 checksum for a given string or byte array
*/
public class CRC32 {
public static void main(String[] args) {
System.out.println(Integer.toHexString(crc32("Hello World")));
}
public static int crc32(String str) {
return crc32(str.getBytes());
}
public static int crc32(byte[] data) {
BitSet bitSet = BitSet.valueOf(data);
int crc32 = 0xFFFFFFFF; // initial value
for (int i = 0; i < data.length * 8; i++) {
if (((crc32 >>> 31) & 1) != (bitSet.get(i) ? 1 : 0))
crc32 = (crc32 << 1) ^ 0x04C11DB7; // xor with polynomial
else
crc32 = (crc32 << 1);
}
crc32 = Integer.reverse(crc32); // result reflect
return crc32 ^ 0xFFFFFFFF; // final xor value
}
}