Created
March 6, 2014 02:42
-
-
Save WalterInSH/9381241 to your computer and use it in GitHub Desktop.
CRC32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static long getCRC32(String _source) throws UnsupportedEncodingException { | |
int crc = 0xFFFFFFFF; // initial contents of LFBSR | |
int poly = 0xEDB88320; // reverse polynomial | |
byte[] bytes = _source.getBytes("utf-8"); | |
for (byte b : bytes) { | |
int temp = (crc ^ b) & 0xff; | |
// read 8 bits one at a time | |
for (int i = 0; i < 8; i++) { | |
if ((temp & 1) == 1) | |
temp = (temp >>> 1) ^ poly; | |
else | |
temp = (temp >>> 1); | |
} | |
crc = (crc >>> 8 ^ temp); | |
} | |
// flip bits | |
crc = crc ^ 0xffffffff; | |
return UnsignedInteger.asUnsigned(crc).longValue(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment