Skip to content

Instantly share code, notes, and snippets.

@WalterInSH
Created March 6, 2014 02:42
Show Gist options
  • Save WalterInSH/9381241 to your computer and use it in GitHub Desktop.
Save WalterInSH/9381241 to your computer and use it in GitHub Desktop.
CRC32
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