From 3e56ea6a99e7da5d61c5956876c2d977268e4756 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 26 Oct 2015 13:11:19 +1300 Subject: Use standard Java hex conversion --- src/org/traccar/helper/ChannelBufferTools.java | 19 +------------------ src/org/traccar/helper/Hashing.java | 9 +++++---- 2 files changed, 6 insertions(+), 22 deletions(-) (limited to 'src/org') 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)); } -- cgit v1.2.3