diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-26 13:11:19 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-26 13:11:19 +1300 |
commit | 3e56ea6a99e7da5d61c5956876c2d977268e4756 (patch) | |
tree | f70f61b1ec218e4491d36c10d5fbd52a67ba4862 | |
parent | fbdd359d223579173b4e0076563df296f5492e3c (diff) | |
download | trackermap-server-3e56ea6a99e7da5d61c5956876c2d977268e4756.tar.gz trackermap-server-3e56ea6a99e7da5d61c5956876c2d977268e4756.tar.bz2 trackermap-server-3e56ea6a99e7da5d61c5956876c2d977268e4756.zip |
Use standard Java hex conversion
9 files changed, 22 insertions, 41 deletions
diff --git a/src/org/traccar/helper/ChannelBufferTools.java b/src/org/traccar/helper/ChannelBufferTools.java index 99da43b54..6c61287c9 100644 --- a/src/org/traccar/helper/ChannelBufferTools.java +++ b/src/org/traccar/helper/ChannelBufferTools.java @@ -17,6 +17,7 @@ package org.traccar.helper; import org.jboss.netty.buffer.ChannelBuffer; +import javax.xml.bind.DatatypeConverter; import java.util.Formatter; public final class ChannelBufferTools { @@ -89,22 +90,4 @@ public final class ChannelBufferTools { return value; } - public static byte[] hexToBytes(String in) { - int count = in.length() / 2; - byte[] out = new byte[count]; - for (int i = 0; i < count; i++) { - out[i] = (byte) Integer.parseInt(in.substring(i * 2, (i + 1) * 2), 16); - } - return out; - } - - public static String bytesToHex(byte[] in) { - StringBuilder out = new StringBuilder(); - Formatter formatter = new Formatter(out); - for (byte b : in) { - formatter.format("%02x", b); - } - return out.toString(); - } - } diff --git a/src/org/traccar/helper/Hashing.java b/src/org/traccar/helper/Hashing.java index cdc5c7ae5..29020f3a7 100644 --- a/src/org/traccar/helper/Hashing.java +++ b/src/org/traccar/helper/Hashing.java @@ -19,6 +19,7 @@ import java.security.GeneralSecurityException; import java.security.SecureRandom; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; +import javax.xml.bind.DatatypeConverter; public final class Hashing { @@ -65,13 +66,13 @@ public final class Hashing { RANDOM.nextBytes(salt); byte[] hash = function(password.toCharArray(), salt); return new HashingResult( - ChannelBufferTools.bytesToHex(hash), - ChannelBufferTools.bytesToHex(salt)); + DatatypeConverter.printHexBinary(hash), + DatatypeConverter.printHexBinary(salt)); } public static boolean validatePassword(String password, String hashHex, String saltHex) { - byte[] hash = ChannelBufferTools.hexToBytes(hashHex); - byte[] salt = ChannelBufferTools.hexToBytes(saltHex); + byte[] hash = DatatypeConverter.parseHexBinary(hashHex); + byte[] salt = DatatypeConverter.parseHexBinary(saltHex); return slowEquals(hash, function(password.toCharArray(), salt)); } diff --git a/test/org/traccar/ProtocolDecoderTest.java b/test/org/traccar/ProtocolDecoderTest.java index 992ade8f8..d91675f10 100644 --- a/test/org/traccar/ProtocolDecoderTest.java +++ b/test/org/traccar/ProtocolDecoderTest.java @@ -11,6 +11,7 @@ import org.traccar.helper.ChannelBufferTools; import org.traccar.model.Device; import org.traccar.model.Position; +import javax.xml.bind.DatatypeConverter; import java.nio.ByteOrder; import java.nio.charset.Charset; import java.text.DateFormat; @@ -104,7 +105,7 @@ public class ProtocolDecoderTest { protected ChannelBuffer binary(ByteOrder endianness, String... data) { return ChannelBuffers.wrappedBuffer( - endianness, ChannelBufferTools.hexToBytes(concatenateStrings(data))); + endianness, DatatypeConverter.parseHexBinary(concatenateStrings(data))); } protected String text(String... data) { diff --git a/test/org/traccar/helper/ChannelBufferToolsTest.java b/test/org/traccar/helper/ChannelBufferToolsTest.java index d34dfe9f7..64f010ea5 100644 --- a/test/org/traccar/helper/ChannelBufferToolsTest.java +++ b/test/org/traccar/helper/ChannelBufferToolsTest.java @@ -26,14 +26,4 @@ public class ChannelBufferToolsTest { assertEquals("1903", result); } - @Test - public void convertHexStringTest() { - assertArrayEquals(new byte[] {0x12, 0x34}, ChannelBufferTools.hexToBytes("1234")); - } - - @Test - public void convertHexByteArray() { - assertEquals("1234", ChannelBufferTools.bytesToHex(new byte[]{0x12, 0x34})); - } - } diff --git a/test/org/traccar/protocol/CityeasyProtocolEncoderTest.java b/test/org/traccar/protocol/CityeasyProtocolEncoderTest.java index e2bfbb19a..19efb7af4 100644 --- a/test/org/traccar/protocol/CityeasyProtocolEncoderTest.java +++ b/test/org/traccar/protocol/CityeasyProtocolEncoderTest.java @@ -6,6 +6,8 @@ import org.junit.Test; import org.traccar.helper.ChannelBufferTools; import org.traccar.model.Command; +import javax.xml.bind.DatatypeConverter; + public class CityeasyProtocolEncoderTest { @Test @@ -18,8 +20,8 @@ public class CityeasyProtocolEncoderTest { command.setType(Command.TYPE_SET_TIMEZONE); command.set(Command.KEY_TIMEZONE, 6 * 3600); - Assert.assertEquals(encoder.encodeCommand(command), ChannelBuffers.wrappedBuffer(ChannelBufferTools.hexToBytes( - "5353001100080001680000000B60820D0A"))); + Assert.assertEquals(encoder.encodeCommand(command), ChannelBuffers.wrappedBuffer( + DatatypeConverter.parseHexBinary("5353001100080001680000000B60820D0A"))); } diff --git a/test/org/traccar/protocol/Gt06ProtocolEncoderTest.java b/test/org/traccar/protocol/Gt06ProtocolEncoderTest.java index 1041e65af..eff1942d1 100644 --- a/test/org/traccar/protocol/Gt06ProtocolEncoderTest.java +++ b/test/org/traccar/protocol/Gt06ProtocolEncoderTest.java @@ -6,6 +6,8 @@ import org.junit.Test; import org.traccar.helper.ChannelBufferTools; import org.traccar.model.Command; +import javax.xml.bind.DatatypeConverter; + public class Gt06ProtocolEncoderTest { @Test @@ -17,8 +19,8 @@ public class Gt06ProtocolEncoderTest { command.setDeviceId(1); command.setType(Command.TYPE_ENGINE_STOP); - Assert.assertEquals(encoder.encodeCommand(command), ChannelBuffers.wrappedBuffer(ChannelBufferTools.hexToBytes( - "787812800c0000000052656c61792c312300009dee0d0a"))); + Assert.assertEquals(encoder.encodeCommand(command), ChannelBuffers.wrappedBuffer( + DatatypeConverter.parseHexBinary("787812800c0000000052656c61792c312300009dee0d0a"))); } diff --git a/test/org/traccar/protocol/KhdProtocolEncoderTest.java b/test/org/traccar/protocol/KhdProtocolEncoderTest.java index 872997d7f..2a3fb524b 100644 --- a/test/org/traccar/protocol/KhdProtocolEncoderTest.java +++ b/test/org/traccar/protocol/KhdProtocolEncoderTest.java @@ -6,6 +6,8 @@ import org.junit.Test; import org.traccar.helper.ChannelBufferTools; import org.traccar.model.Command; +import javax.xml.bind.DatatypeConverter; + public class KhdProtocolEncoderTest { @Test @@ -17,8 +19,8 @@ public class KhdProtocolEncoderTest { command.setDeviceId(1); command.setType(Command.TYPE_ENGINE_STOP); - Assert.assertEquals(encoder.encodeCommand(command), ChannelBuffers.wrappedBuffer(ChannelBufferTools.hexToBytes( - "2929390006000000003F0D"))); + Assert.assertEquals(encoder.encodeCommand(command), ChannelBuffers.wrappedBuffer( + DatatypeConverter.parseHexBinary("2929390006000000003F0D"))); } diff --git a/test/org/traccar/protocol/SkypatrolProtocolDecoderTest.java b/test/org/traccar/protocol/SkypatrolProtocolDecoderTest.java index 9c7d79072..d3a9de340 100644 --- a/test/org/traccar/protocol/SkypatrolProtocolDecoderTest.java +++ b/test/org/traccar/protocol/SkypatrolProtocolDecoderTest.java @@ -13,7 +13,7 @@ public class SkypatrolProtocolDecoderTest extends ProtocolDecoderTest { SkypatrolProtocolDecoder decoder = new SkypatrolProtocolDecoder(new SkypatrolProtocol()); verifyPosition(decoder, binary( - "0005021004FFFFFFFF0000000D313134373735383300CB000000000E11070C010184D032FB3841370000000016072B000017050032000000000000024E0C071116072C105900050000000000050000000000050000000003100260B7363B6306C11A00B73637F206BF19B73637F106B50EB73638B106BB0BB7363B6106B80AB73637F306B709000000000000000000C")); + "0005021004FFFFFFFF0000000D313134373735383300CB000000000E11070C010184D032FB3841370000000016072B000017050032000000000000024E0C071116072C105900050000000000050000000000050000000003100260B7363B6306C11A00B73637F206BF19B73637F106B50EB73638B106BB0BB7363B6106B80AB73637F306B709000000000000000000")); //verifyPosition(decoder, binary( // "000500030101383637383434303031373832333336420102000c0000fa07b5e101876c5b0e0a111606131c1b5e")); diff --git a/test/org/traccar/protocol/XexunFrameDecoderTest.java b/test/org/traccar/protocol/XexunFrameDecoderTest.java index e9a79ab45..532554225 100644 --- a/test/org/traccar/protocol/XexunFrameDecoderTest.java +++ b/test/org/traccar/protocol/XexunFrameDecoderTest.java @@ -13,7 +13,7 @@ public class XexunFrameDecoderTest extends ProtocolDecoderTest { Assert.assertEquals( binary("4750524d432c3230353933352e3030302c412c353134302e343335302c4e2c3530312e303638362c452c302e30302c302e30302c3132313031352c30302c303030302e302c412a37302c462c2c696d65693a3335393538373031343731383339322c"), - decoder.decode(null, null, binary("313531303132313435392c2b33313635323435343932372c4750524d432c3230353933352e3030302c412c353134302e343335302c4e2c3530312e303638362c452c302e30302c302e30302c3132313031352c30302c303030302e302c412a37302c462c2c696d65693a3335393538373031343731383339322c313232497"))); + decoder.decode(null, null, binary("313531303132313435392c2b33313635323435343932372c4750524d432c3230353933352e3030302c412c353134302e343335302c4e2c3530312e303638362c452c302e30302c302e30302c3132313031352c30302c303030302e302c412a37302c462c2c696d65693a3335393538373031343731383339322c31323249"))); Assert.assertEquals( binary("4750524d432c3130333733312e3633362c412c343534352e353236362c4e2c30303434382e383235392c452c32312e31322c3237362e30312c3135303631352c2c2c412a35372c4c2c2c20696d65693a3031333934393030323032363637352c"), |