@Test public void testCalculateOspfCheckSum() throws Exception { result = checksumCalculator.calculateOspfCheckSum(helloPacket, ospfChecksumPos1, ospfChecksumPos2); assertThat(result, is(notNullValue())); result = checksumCalculator.calculateOspfCheckSum(updatePacket, ospfChecksumPos1, ospfChecksumPos2); assertThat(result, is(notNullValue())); }
public byte[] calculateOspfCheckSum(byte[] packet, int checksumPos1, int checksumPos2) { int hexasum = 0; for (int i = 0; i < packet.length; i = i + 2) { if (i != 12) { byte b1 = packet[i]; String s1 = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '0'); b1 = packet[i + 1]; String s2 = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '0'); String hexa = s1 + s2; int num1 = Integer.parseInt(hexa, 2); hexasum = hexasum + num1; String convertTo16 = Integer.toHexString(hexasum); if (convertTo16.length() > 4) { hexasum = convertToSixteenBits(convertTo16); } } } StringBuilder sb = new StringBuilder(Integer.toHexString(hexasum)); if (sb.length() > 4) { sb = sb.reverse(); StringBuilder s1 = new StringBuilder(sb.substring(0, 4)); s1 = s1.reverse(); StringBuilder s2 = new StringBuilder(sb.substring(4, sb.length())); s2 = s2.reverse(); hexasum = Integer.parseInt(s1.toString(), 16) + Integer.parseInt(s2.toString(), 16); } int finalChecksum = calculateChecksum(hexasum); return OspfUtil.convertToTwoBytes(finalChecksum); }
ChecksumCalculator { public byte[] calculateOspfCheckSum(byte[] packet, int checksumPos1, int checksumPos2) { int hexasum = 0; for (int i = 0; i < packet.length; i = i + 2) { if (i != 12) { byte b1 = packet[i]; String s1 = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '0'); b1 = packet[i + 1]; String s2 = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '0'); String hexa = s1 + s2; int num1 = Integer.parseInt(hexa, 2); hexasum = hexasum + num1; String convertTo16 = Integer.toHexString(hexasum); if (convertTo16.length() > 4) { hexasum = convertToSixteenBits(convertTo16); } } } StringBuilder sb = new StringBuilder(Integer.toHexString(hexasum)); if (sb.length() > 4) { sb = sb.reverse(); StringBuilder s1 = new StringBuilder(sb.substring(0, 4)); s1 = s1.reverse(); StringBuilder s2 = new StringBuilder(sb.substring(4, sb.length())); s2 = s2.reverse(); hexasum = Integer.parseInt(s1.toString(), 16) + Integer.parseInt(s2.toString(), 16); } int finalChecksum = calculateChecksum(hexasum); return OspfUtil.convertToTwoBytes(finalChecksum); } }
ChecksumCalculator { public byte[] calculateOspfCheckSum(byte[] packet, int checksumPos1, int checksumPos2) { int hexasum = 0; for (int i = 0; i < packet.length; i = i + 2) { if (i != 12) { byte b1 = packet[i]; String s1 = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '0'); b1 = packet[i + 1]; String s2 = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '0'); String hexa = s1 + s2; int num1 = Integer.parseInt(hexa, 2); hexasum = hexasum + num1; String convertTo16 = Integer.toHexString(hexasum); if (convertTo16.length() > 4) { hexasum = convertToSixteenBits(convertTo16); } } } StringBuilder sb = new StringBuilder(Integer.toHexString(hexasum)); if (sb.length() > 4) { sb = sb.reverse(); StringBuilder s1 = new StringBuilder(sb.substring(0, 4)); s1 = s1.reverse(); StringBuilder s2 = new StringBuilder(sb.substring(4, sb.length())); s2 = s2.reverse(); hexasum = Integer.parseInt(s1.toString(), 16) + Integer.parseInt(s2.toString(), 16); } int finalChecksum = calculateChecksum(hexasum); return OspfUtil.convertToTwoBytes(finalChecksum); }  }
ChecksumCalculator { public byte[] calculateOspfCheckSum(byte[] packet, int checksumPos1, int checksumPos2) { int hexasum = 0; for (int i = 0; i < packet.length; i = i + 2) { if (i != 12) { byte b1 = packet[i]; String s1 = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '0'); b1 = packet[i + 1]; String s2 = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '0'); String hexa = s1 + s2; int num1 = Integer.parseInt(hexa, 2); hexasum = hexasum + num1; String convertTo16 = Integer.toHexString(hexasum); if (convertTo16.length() > 4) { hexasum = convertToSixteenBits(convertTo16); } } } StringBuilder sb = new StringBuilder(Integer.toHexString(hexasum)); if (sb.length() > 4) { sb = sb.reverse(); StringBuilder s1 = new StringBuilder(sb.substring(0, 4)); s1 = s1.reverse(); StringBuilder s2 = new StringBuilder(sb.substring(4, sb.length())); s2 = s2.reverse(); hexasum = Integer.parseInt(s1.toString(), 16) + Integer.parseInt(s2.toString(), 16); } int finalChecksum = calculateChecksum(hexasum); return OspfUtil.convertToTwoBytes(finalChecksum); }  static int convertToSixteenBits(String strToConvert); boolean isValidOspfCheckSum(OspfMessage ospfMessage, int checksumPos1, int checksumPos2); boolean isValidLsaCheckSum(OspfLsa ospfLsa, int lsType, int lsaChecksumPos1,
                                      int lsaChecksumPos2); boolean validateLsaCheckSum(byte[] lsaPacket, int lsaChecksumPos1, int lsaChecksumPos2); boolean validateOspfCheckSum(byte[] ospfPacket, int checksumPos1, int checksumPos2); byte[] calculateLsaChecksum(byte[] lsaBytes, int lsaChecksumPos1, int lsaChecksumPos2); byte[] calculateOspfCheckSum(byte[] packet, int checksumPos1, int checksumPos2); }
ChecksumCalculator { public byte[] calculateOspfCheckSum(byte[] packet, int checksumPos1, int checksumPos2) { int hexasum = 0; for (int i = 0; i < packet.length; i = i + 2) { if (i != 12) { byte b1 = packet[i]; String s1 = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '0'); b1 = packet[i + 1]; String s2 = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '0'); String hexa = s1 + s2; int num1 = Integer.parseInt(hexa, 2); hexasum = hexasum + num1; String convertTo16 = Integer.toHexString(hexasum); if (convertTo16.length() > 4) { hexasum = convertToSixteenBits(convertTo16); } } } StringBuilder sb = new StringBuilder(Integer.toHexString(hexasum)); if (sb.length() > 4) { sb = sb.reverse(); StringBuilder s1 = new StringBuilder(sb.substring(0, 4)); s1 = s1.reverse(); StringBuilder s2 = new StringBuilder(sb.substring(4, sb.length())); s2 = s2.reverse(); hexasum = Integer.parseInt(s1.toString(), 16) + Integer.parseInt(s2.toString(), 16); } int finalChecksum = calculateChecksum(hexasum); return OspfUtil.convertToTwoBytes(finalChecksum); }  static int convertToSixteenBits(String strToConvert); boolean isValidOspfCheckSum(OspfMessage ospfMessage, int checksumPos1, int checksumPos2); boolean isValidLsaCheckSum(OspfLsa ospfLsa, int lsType, int lsaChecksumPos1,
                                      int lsaChecksumPos2); boolean validateLsaCheckSum(byte[] lsaPacket, int lsaChecksumPos1, int lsaChecksumPos2); boolean validateOspfCheckSum(byte[] ospfPacket, int checksumPos1, int checksumPos2); byte[] calculateLsaChecksum(byte[] lsaBytes, int lsaChecksumPos1, int lsaChecksumPos2); byte[] calculateOspfCheckSum(byte[] packet, int checksumPos1, int checksumPos2);  }