Skip to content

Commit

Permalink
fix DigestUtil by using unsigned int conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Mar 16, 2018
1 parent eacca90 commit 1ba0e8b
Showing 1 changed file with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,17 @@ Licensed to the Apache Software Foundation (ASF) under one

public class DigestUtil {

private static CRC crc16 = new CRC(CRC.Parameters.CRC16);
public static final CRC.Parameters CRC16 = CRC.Parameters.CRC16;

public static final CRC.Parameters CRC16_ADS = new CRC.Parameters(
CRC16.getWidth(),
CRC16.getPolynomial(),
0xFFFF,
CRC16.isReflectIn(),
CRC16.isReflectOut(),
CRC16.getFinalXor());

private static CRC crc16 = new CRC(CRC16_ADS);

private DigestUtil() {
// Utility class
Expand All @@ -37,11 +47,13 @@ public static int calculateCrc16(ByteReadable... byteReadables) {
for (ByteReadable byteReadable : byteReadables) {
currentCrcValue = crc16.update(currentCrcValue, byteReadable.getBytes());
}
return crc16.finalCRC16(currentCrcValue) & 0x0000FFFF;
short finalCrc = crc16.finalCRC16(currentCrcValue);
return Short.toUnsignedInt(finalCrc);
}

public static int calculateCrc16(byte[] bytes) {
return (int) crc16.calculateCRC(bytes) & 0x0000FFFF;
short crc = (short) crc16.calculateCRC(bytes);
return Short.toUnsignedInt(crc);
}

}

0 comments on commit 1ba0e8b

Please sign in to comment.